Check 404 error status in jQuery

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 the correct URL, this code works fine, the ei success object executes, but if the URL is incorrect, ei Error 404 is returned from twitter, the error object never executes. It does not write anything to the console. How to check the status of error 404 in this case?

+5
source share
2 answers

From the 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/

+3
source

statusCode .ajax.

$.ajax({
  ...
  statusCode: {
    404: function(){
    }
  }
});
+2

All Articles