2

Please consider the following JavaScript

$.ajax({
    url: 'http://www.example.com/jsfunc/mycallback',
    cache: true,
    dataType: 'jsonp',
    jsonp: false, // defined by jsfunc/ argument
    jsonpCallback: "mycallback",
    timeout: 10000,
    error: function(jqXHR, textStatus, errorThrown) {
        window.console.log('JSONP Error: ' + textStatus + ' ' + errorThrown);
    },
    complete: function(jqXHR, textStatus) {
        window.console.log('JSONP was retrieved successfully');
    }
});

window.mycallback = function(data) {
    window.console.log('Callback with ' + data);
}

In standard operation the URL is requested, mycallback hit and the complete function run.

The problem I have is on occasion the URL returns data that triggers the error parsererror mycallback was not called. I'd like to log the data that is returned, obviously this data does not contain mycallback({[...]}) as the callback has not been triggered. As the situation only occurs on occasion under heavy load and I don't know what the data is the service is returning I'd like to log it so I can debug it.

I've looked at jqXHR.responseText and jqXHR.responseXML and both these are empty.

If the data returned by a jQuery JSONP request doesn't call the callback how can it be retrieved?

Many thanks in advance,
Pete

Peter Hough
  • 560
  • 4
  • 17
  • Have you debugged the entire jqXHR object to see what it contains? It sounds like jqXHR object doesn't populate its response properties if there is an error... which makes things difficult for you. – jeremyharris May 31 '12 at 15:06
  • @Greg I'm specifying the jsonpCallback and defining the function separately. I'm not sure how the answer to that question is related? – Peter Hough May 31 '12 at 16:06
  • @jeremyharris Yes I did look through jqXHR and couldn't see anything. You understand my difficulties. I did think about maybe requesting the data as text/plain then trying to parse and handle the errors manually but I need to cross domain functionality JSONP provides. – Peter Hough May 31 '12 at 16:08
  • @PeterHough Sorry, don't know what I was thinking. Deleted. – gcochard May 31 '12 at 16:17
  • 1
    @PeterHough So I looked at the jQuery source and I don't think it's possible, as it doesn't store the original response or AFAIK provide a way to get the container where the response was originally held before it wipes it from the dom. Sorry :/ That said, the reason the request is failing is because there's no response in the first place so I think trying to get it is pointless. – jeremyharris May 31 '12 at 16:46
  • @jeremyharris Thanks for your help Jeremy. A bit disappointing that jQuery doesn't provide a way to get the response. The request is failing because the response isn't JSON, not necessarily because there is no response. I actually suspect a HTML error page is being returned so my thinking was to capture that so it can be debugged and/or report a bug in the third party service provider. – Peter Hough Jun 01 '12 at 09:01
  • Yeah I saw another post *similar* to this and it turned out they were using a third party API as well. It's quite a bummer you can't have access to the raw response. Maybe a jQuery feature request is in order. – jeremyharris Jun 01 '12 at 18:04
  • Try this http://stackoverflow.com/questions/5359224/parsererror-after-jquery-ajax-request-with-jsonp-content-type – kiranvj Mar 04 '13 at 06:29

0 Answers0