Today I had to deal with the same precedent. The application I'm working on has these long ajax calls that can be interrupted: 1) by the user performing the navigation, or 2) some temporary connection / server failure. I want the error handler to be run only for connection / server failure, and not for user transition.
At first I tried to answer Alastair Pitts, but this did not work, because both the interrupted requests and the connection failure code set the status code and readyState to 0. Next, I tried sieppl; also did not work, because in both cases there was no answer, so there is no header.
The only solution that worked for me was to set a listener for window.onbeforeunload, which sets a global variable indicating that the page has been unloaded. The error handler can 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; } });
bluecollarcoder Feb 28 '13 at 16:50 2013-02-28 16:50
source share