views:

10751

answers:

8

Is it possible to catch an error when using JSONP with jQuery? I've tried both the $.getJSON and $.ajax methods but neither will catch the 404 error I'm testing. Here is what I've tried (keep in mind that these all work successfully, but I want to handle the case when it fails):

jQuery.ajax({
    type: "GET",
    url: handlerURL,
    dataType: "jsonp",
    success: function(results){
        alert("Success!");
    },
    error: function(XMLHttpRequest, textStatus, errorThrown){
        alert("Error");
    }
});

And also:

jQuery.getJSON(handlerURL + "&callback=?", 
    function(jsonResult){
        alert("Success!");
    });

I've also tried adding the $.ajaxError but that didn't work either:

jQuery(document).ajaxError(function(event, request, settings){
   alert("Error");
});

Thanks in advance for any replies!

+7  A: 

It seems that JSONP requests that don't return a successful result never trigger any event, success or failure, and for better or worse that's apparently by design.

After searching their bug tracker, there's a patch which may be a possible solution using a timeout callback. See bug report #3442. If you can't capture the error, you can at least timeout after waiting a reasonable amount of time for success.

Adam Bellaire
A: 

http://code.google.com/p/jquery-jsonp/

Julian Aubourg
-1 the provided link is dead
Ryan Delucchi
Project has been deleted (after having been deprecated for months) and replaced by the one I linked to now (edited my post).Now that's quite a harsh reaction to a broken link :P
Julian Aubourg
And I now realize livedo had put it before... oh my :/
Julian Aubourg
A: 

like me! unfortunately the getJSON project on google code is no longer up, but thanks for the link to the patch!

A: 

hi there,

is this patch still available somewhere?

thanks and regards, tk

+2  A: 

A working URI for Julian's solution for JSON-P with jQuery is http://code.google.com/p/jquery-jsonp/

livedo
A: 

Seems like this is working now: jQuery(document).ajaxError(function(event, request, settings){ alert("Error"); });

Fhansen
A: 

If you collaborate with the provider, you could send another query string parameter being the function to callback when there's an error.

?callback=?&error=?

This is called JSONPE but it's not at all a defacto standard.

The provider then passes information to the error function to help you diagnose.

Doesn't help with comm errors though - jQuery would have to be updated to also callback the error function on timeout, as in Adam Bellaire's answer.

delitescere
+1  A: 

A working solution for this is in a seperate answer: http://stackoverflow.com/questions/572991/jquery-getjson-doesnt-trigger-callback/573044#573044

Call this function before your jquery.ajax (or jquery.getJSON etc) call:

$.ajaxSetup({
  "error":function() {   
    //reset state here;
}});

Then call your normal function afterwards:

$.getJSON('ajax/test.json', function(data) {
  $('.result').html('<p>' + data.foo + '</p>' + '<p>' + data.baz[1] + '</p>');
});
The Artful Benny