5

I use this code to get some information from twitter via their api:

        $.ajax({
            url : apiUrl,
            cache : false,
            crossDomain: true,
            dataType: "jsonp",
            success : function(html) {
                // ...
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(jqXHR);
            }
        });

However, if the apiUrl variable provides a correct url, this code work fine, e.i. the success object is executed, but if the url isn't correct, e.i. 404 error is returned from twitter, the error object is never executed. It doesn't log anything in console. How should I check for 404 error status in this case?

King Julien
  • 10,981
  • 24
  • 94
  • 132

2 Answers2

3

From jQuery API ajax docs:

error option

Note: This handler is not called for cross-domain script and JSONP requests.

http://api.jquery.com/jQuery.ajax/

charlietfl
  • 170,828
  • 13
  • 121
  • 150
2

According to the docs, use statusCode setting in .ajax.

$.ajax({
  ...
  statusCode: {
    404: function(){
    }
  }
});
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • nop, this isn't executed as well. I'm afraid the first reply to this topic is correct... http://forum.jquery.com/topic/jquery-ajax-with-datatype-jsonp-will-not-use-error-callback-if-request-fails – King Julien Mar 02 '12 at 14:03