I have the following code for default jQuery AJAX error handling:
$.ajaxSetup({
    error : function(jqXHR, textStatus, errorThrown) {
        alert("Error: " + textStatus + ": " + errorThrown);
    },
    statusCode : {
        404: function() {
            alert("Element not found.");
        }
    }
});
However, when 404 happens, BOTH functions are upcalled: first error, then statusCode, so I see 2 consecutive alerts.
How to prevent this behaviour and get error callback only if statusCode was not upcalled?
How about just checking for status code 404 in your error handler?
$.ajaxSetup({
    error : function(jqXHR, textStatus, errorThrown) {
        if (jqXHR.status == 404) {
            alert("Element not found.");
        } else {
            alert("Error: " + textStatus + ": " + errorThrown);
        }
    }
});
                        Try this:
$.ajaxSetup({
error : function(jqXHR, textStatus, errorThrown) {
    if(jqXHR.status === 404) {
      alert("Element not found.");
    } else {
      alert("Error: " + textStatus + ": " + errorThrown);
    }
}
});
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With