Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax: How to prevent jQuery from calling the success event after executing xmlHttpRequest.abort();

Using jQuery's $.ajax() function. Wherether the request has been purposely aborted, or if the server is down (not responding) it appears the same outcome happens;

That is the "success" handler gets triggered where xmlHttpRequest.status = 0 and xmlHttpRequest.readyState = 4.

(I simulated the failed request by shutting off IIS, and then executing a xmlHttpRequest against the website that had been "turned off")

So my question is how can I determine the difference between an aborted request, or a request that genuinely failed due to the server not responding (because maybe the server is down), since both scenarios appear to give me the same status/readyState?

EDIT More accurately I want to know how to prevent calling the "success" handler after the .abort() function is called on ajax.

I have re-worded the question to reflect this.

like image 415
7wp Avatar asked Mar 01 '10 16:03

7wp


4 Answers

jQuery < 1.5

For jQuery < 1.5 I have come up with the following solution:

var request = $.ajax( /* ... */ );

// ...

request.onreadystatechange = function () {};
request.abort();

So, basically what I do, is to remove the success callback handler before calling abort().

This works like a charm.

jQuery >= 1.5:

Starting with jQuery 1.5 the $.ajax(), $.get(), … functions return the jXHR object (api documentation) instead of the xmlHttpRequest object. Hence you can not simply overwrite the xmlHttpRequest.onreadystatechange() handler.

This said, the jXHR.abort() takes care of not calling the success callback handler. Hence it is sufficient to call jXHR.abort().

You do necessarily not need to update your previous code, as setting an onreadystatechange to jXHR will just have no effect at all.

Long story short, startin with jQuery 1.5, this will do:

var jxhr = $.ajax( /* ... */ );

// ...

jxhr.abort();
like image 162
Pierre Spring Avatar answered Oct 23 '22 05:10

Pierre Spring


The best solution to have only one request is assigning the return value of your $.ajax call to a variable, e.g. currentRequest, and then, inside your success(data, responseCode, xhr) function, checking if(xhr == currentRequest) and returning early if the check failed. When aborting you simply set currentRequest to null so the check fails.

This ensures an old request is never handled which is especially important if you do e.g. long polling and restart the request as soon as it has finished (you really don't want two "pollers" running at the same time due to an aborted request starting a new one).

like image 29
ThiefMaster Avatar answered Oct 23 '22 05:10

ThiefMaster


You should set up an error handler, which will get called if the request fails. If you want to abort the request on the server, you could just return some value that you can check for in your success handler, or you could throw an exception from the server. jquery.ajax()

EDIT: You may want to look into this AJAX Queue/Cache/Abort/Block Manager v. 2.0 (link). It will allow you to run a function after a request has been aborted.

like image 20
Matt Dearing Avatar answered Oct 23 '22 03:10

Matt Dearing


I've noticed this same behavior, and request.onreadystatechange = function () {}; before calling .abort() does resolve the issue.

like image 42
user463122 Avatar answered Oct 23 '22 04:10

user463122