JQuery Ajax error handling to ignore interruption

I want to have a global error handling method for ajax calls, here is what I have now:

$.ajaxSetup({ error: function (XMLHttpRequest, textStatus, errorThrown) { displayError(); } }); 

I need to ignore the aborted error. errorThrown is null and textStatus is error . How to check aborted ?

+66
jquery ajax error-handling
Jan 26 '11 at 17:11
source share
8 answers

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; } }); 
+45
Feb 28 '13 at 16:50
source share

In modern jQuery, you can simply check if request.statusText 'abort' :

 error: function (request, textStatus, errorThrown) { if (request.statusText =='abort') { return; } } 
+29
Nov 30 '14 at 9:23
source share

I found that when there is an interrupted request, status and / or readyState are 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 not a true error and we don't care if (jqXHR.status === 0 || jqXHR.readyState === 0) { return; } //Do Stuff Here }); 

I found this to work fine for me. Hope this helps you or anyone else who comes across this :)

+21
May 11 '11 at 2:54
source share

You need to look at the textStatus argument passed to your error function. According to http://api.jquery.com/jQuery.ajax/ , it can take the values ​​“success”, “unmodified”, “error”, “timeout”, “interrupt”, or “parsererror”. "abort" is obviously what you want to check.

Longer notes here: jquery-gotcha-error-callback-triggered-on-xhr-abort

+10
Oct 11
source share

Since bluecollarcoders answer doesn't work for ajax requests interrupted by javascript, here is my solution:

 var unloaded = false; ... $(window).bind('beforeunload', function(){ unloaded = true; }); $(document).ajaxError(function(event, request, settings) { if (unloaded || request.statusText == "abort") { return; } ... } 

eg.

 handler = jQuery.get("foo") handler.abort() 

will now be ignored by the ajaxError handler

+3
Apr 07 '14 at 16:17
source share
 $(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) { if (!jqXHR.getAllResponseHeaders()) { return; } }); 
+2
Jul 05 2018-12-12T00:
source share

Based on Alastair Pitts's answer , you can also do this to get more informative messages:

 $(document).ajaxError(function (e, jqXHR, ajaxSettings, thrownError) { { if (jqXHR.status === 0) { alert('Not connect.\n Verify Network.'); } else if (jqXHR.status == 404) { alert('Requested page not found. [404]'); } else if (jqXHR.status == 500) { alert('Internal Server Error [500].'); } else if (exception === 'parsererror') { alert('Requested JSON parse failed.'); } else if (exception === 'timeout') { alert('Time out error.'); } else if (exception === 'abort') { alert('Ajax request aborted.'); } else { alert('Uncaught Error.\n' + jqXHR.responseText); } } }); 
+2
Aug 30 '13 at 22:03
source share

I had the same problem, and I decided how to “abort” var before calling the abort () function, as shown below:

 aborting = true; myAjax.abort(); 

and show only an error in the ajax request error handler if aborting it incorrectly.

 $.ajax({ [..] error: function() { if ( !aborting ) { // do some stuff.. } aborting = false; } }); 
0
Mar 23 '14 at 23:52
source share



All Articles