I want to have a global error handling method for ajax calls, this is what I have now:
$.ajaxSetup({
error: function (XMLHttpRequest, textStatus, errorThro
You'll want to look at the textStatus argument passed into your error function. According to http://api.jquery.com/jQuery.ajax/, it can take the values "success", "notmodified", "error", "timeout", "abort", or "parsererror". "abort" is obviously what you want to check against.
Longer notes here: jquery-gotcha-error-callback-triggered-on-xhr-abort
$(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) {
if (!jqXHR.getAllResponseHeaders()) {
return;
}
});
Something I found is that when there is an aborted request, the status
and/or readyState
equal 0
.
In my global error handler, I have a check at the top of the method:
$(document).ajaxError(function (e, jqXHR, ajaxSettings, thrownError) {
//If either of these are true, then it's not a true error and we don't care
if (jqXHR.status === 0 || jqXHR.readyState === 0) {
return;
}
//Do Stuff Here
});
I've found this works perfectly for me. Hope this helps you, or anyone else who runs into this :)