views:

578

answers:

6

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.

+2  A: 

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.

Matt Dearing
The request is not aborted on the server.. it is aborted in the client JavaScript using xmlHttpRequest.abort(). and in both scenarios that I tested, the error handler does not get triggered, instead the success handler gets triggered, but with the status/readyState of 0 and 4.
Roberto Sebestyen
Sorry I did not know that you were calling abort on the requests client side. I have edited my answer to be more helpful.
Matt Dearing
Thanks. I have tried that, It still does not solve my problem. Although it allows me to register code to run "onabort", it still triggers the "success" event.
Roberto Sebestyen
@mdearing06 I tried the "AJAX Queue/Cache/Abort/Block Manager" you suggested, but it appears to be buggy. Even the on-line demos are throwing errors (click 3 second delay, then "abort"). No matter, I ended up writing my own Queue Manager. I'll mark your answer as accepted since it pointed me into the right general direction to arrive at my current solution.
Roberto Sebestyen
+1  A: 

You could track aborts separately in a wrapper around xmlHttpRequest.abort.

SLaks
A: 

When is abort called. I just tested in firebug console with the following

var req = $.ajax({url:"localaddress", success:function(){alert("evil");}}); req.abort();

ie abort right after the request, success isn't triggered.

coolnalu
it is called whenever the user hits a particular button on the page more than once. the idea is that the initial request would get aborted before starting a new one. are you sure in your example? Maybe your error handler is being triggered instead because if you are posting to a non existing/non working URL?
Roberto Sebestyen
+3  A: 

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.

Pierre Spring
Thanks i'm gonna give this a try.
Roberto Sebestyen
+1  A: 

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

related questions