I do lots of $.ajax calls, and I handle errors from them in a way that throws up a message. I find that if an ajax call is in progress while the page gets reloaded, e.g. click
The better way without timeouts and other magic flags is to check xhr headers. If no headers, then response didn't come from server, then request was aborted.
var isUserAbortedRequest = function (xhr) {
return !xhr.getAllResponseHeaders();
}
ajaxRequest
.fail(function (xhr, error, statusText) {
console.log("request aborted = ", isUserAbortedRequest(xhr));
})
.success(...)
You can wrap your ajax request with $.Defered like below and use defered object's done\fail.
$.Deferred(function (def) {
ajaxRequest
.fail(function (xhr, error, statusText) {
console.log("request aborted = ", isUserAbortedRequest(xhr));
def.reject({ aborted: isUserAbortedRequest(xhr), error: error });
})
.success(function (response) {
if (response.success == true) {
def.resolve(response.data);
}
else {
def.reject({ aborted: false, error: response.error });
}
});
});