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.
jQuery < 1.5
For jQuery < 1.5 I have come up with the following solution:
So, basically what I do, is to remove the
successcallback handler before callingabort().This works like a charm.
jQuery >= 1.5:
Starting with jQuery 1.5 the
$.ajax(), $.get(), …functions return thejXHRobject (api documentation) instead of thexmlHttpRequestobject. Hence you can not simply overwrite thexmlHttpRequest.onreadystatechange()handler.This said, the
jXHR.abort()takes care of not calling thesuccesscallback handler. Hence it is sufficient to calljXHR.abort().You do necessarily not need to update your previous code, as setting an
onreadystatechangetojXHRwill just have no effect at all.Long story short, startin with jQuery 1.5, this will do: