To add some basic error handling, I wanted to rewrite a piece of code that used jQuery's $.getJSON to pull in some photo's from Flickr. The reason for doing this is that $.getJSON doesn't provide error handling or work with timeouts.
Since $.getJSON is just a wrapper around $.ajax I decided to rewrite the thing and surprise surprise, it works flawlessly.
Now the fun starts though. When I deliberately cause a 404 (by changing the URL) or cause the network to timeout (by not being hooked up to the interwebs), the error event doesn't fire, at all. I'm at a loss as to what I'm doing wrong. Help is much appreciated.
Here's the code:
$(document).ready(function(){
// var jsonFeed = "http://api.flickr.com/services/feeds/photos_public.gne"; // correct URL
var jsonFeed = "http://api.flickr.com/services/feeds/photos_public.gne_______"; // this should throw a 404
$.ajax({
url: jsonFeed,
data: { "lang" : "en-us",
"format" : "json",
"tags" : "sunset"
},
dataType: "jsonp",
jsonp: "jsoncallback",
timeout: 5000,
success: function(data, status){
$.each(data.items, function(i,item){
$("<img>").attr("src", (item.media.m).replace("_m.","_s."))
.attr("alt", item.title)
.appendTo("ul#flickr")
.wrap("<li><a href=\"" + item.link + "\"></a></li>");
if (i == 9) return false;
});
},
error: function(XHR, textStatus, errorThrown){
alert("ERREUR: " + textStatus);
alert("ERREUR: " + errorThrown);
}
});
});
I'd like to add that this question was asked when jQuery was at version 1.4.2
The jquery-jsonp jQuery plugin mentioned in Jose Basilio's answer can now be found on GitHub.
Unfortunately the documentation is somewhat spartan, so I've provided a simple example:
Important Include the callbackParameter in the call $.jsonp() otherwise I've found that the callback function never gets injected into the query string of the service call (current as of version 2.4.0 of jquery-jsonp).
I know this question is old, but the issue of error handling using JSONP is still not 'resolved'. Hopefully this update will assist others as this question is the top-ranked within Google for "jquery jsonp error handling".
jQuery 1.5 and higher have better support for error handling with JSONP requests. However, you need to use the
$.ajax
method instead of$.getJSON
. For me, this works:The timeout seems to do the trick and call the error handler, when there is no successful request after 10 seconds.
I did a little blogpost on this subject as well.
This may be a "known" limitation of jQuery; however, it does not seem to be well documented. I spent about 4 hours today trying to understand why my timeout was not working.
I switched to jquery.jsonp and it worked liked a charm. Thank you.
What about listening to the script's onerror event ? I had this problem and solved it by patching jquery's method where the script tag was created. Here is the interesting bit of code :
What do you think of this solution ? Is it likely to cause compatibility issues ?
This is a known limitation with the native jsonp implementation in jQuery. The text below is from IBM DeveloperWorks
However there's a jsonp plug-in available on GoogleCode that provides support for error handling. To get started, just make the following changes to your code.
You can either download it, or just add a script reference to the plug-in.
Then modify your ajax call as shown below:
A solution if you're stuck with jQuery 1.4: