I want to have a global error handling method for ajax calls, this is what I have now:
$.ajaxSetup({
error: function (XMLHttpRequest, textStatus, errorThro
I had to deal with the same use case today. The app I am working on has these long-running ajax calls that can be interrupted by 1) the user navigating away or 2) some kind of temporary connection/server failure. I want the error handler to run only for connection/server failure and not for the user navigating away.
I first tried Alastair Pitts' answer, but it did not work because both aborted requests and connection failure set status code and readyState to 0. Next, I tried sieppl's answer; also did not work because in both cases, no response is given, thus no header.
The only solution that worked for me is to set a listener for window.onbeforeunload, which sets a global variable to indicate that the page has been unloaded. The error handler can then check and only call the error handler only if the page has not been unloaded.
var globalVars = {unloaded:false};
$(window).bind('beforeunload', function(){
globalVars.unloaded = true;
});
...
$.ajax({
error: function(jqXHR,status,error){
if (globalVars.unloaded)
return;
}
});