6

Global Ajax event handlers that are attached with jQuery.ajaxError or jQuery.ajaxComplete don't seem to receive any information about whether a fetch failure is as a result of a time out. Any pointers on how I can detect time outs? Would checking to see if the status property of the jqXHR object is 0 be a reliable method?

Ates Goral
  • 137,716
  • 26
  • 137
  • 190

1 Answers1

9

Probably there are some differences how you defined the global jQuery Ajax event handler. You don't posted your code. I tried the following

$(document).bind("ajaxError", function (e, jqXHR, ajaxSettings, thrownError) {
    // log the event
});
$(document).bind("ajaxComplete", function (e, jqXHR) {
    // log the event
});

with jQuery 1.7.1 and I can see that in case of timeout error always both ajaxError and ajaxComplete was called.

You can verify this in the simple demo which display in my tests either

enter image description here

in case of success or

enter image description here

on timeout error.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Hi Oleg. In my findings I'm not able to get the specific error coming back as "timeout" from inducing a 408 header. Rather, I get a message "error" which is very general. How did you have this consistent "timeout" error come through? Thanks. – King Friday Dec 19 '11 at 09:18
  • Actually, I just noticed how you did it. You didn't actually do it at all, you placed an artificial timeout. In the real world, you would determine the server timeout differently. – King Friday Dec 19 '11 at 09:27
  • 1
    @emeraldcode.com: The "Timeout" can be interpreted in different ways. In the question one wrote about "status property of the jqXHR object is 0". It is typical for the *client-side* timeout. In case if the status code `408` the problem seems me more simple. One receive just an error and can examine whether the status code is 408. Moreover in the question one ask specially about the problems in *global* event handler, so one can suppose that the problem to catch `timeout` in the `error` handler not exist. – Oleg Dec 19 '11 at 09:40
  • Yes, it could interpreted that way, you are correct. However, the approach I prefer is first getting an actual error, then inspecting. In this scenario its possible to have a transaction go through but give an error back prior to its completion to the client. – King Friday Dec 19 '11 at 16:19
  • the main problem is you cannot get the error code from the response it seems after playing around with this. The only way to get the result you have is by setting the timeout in the ajax call. – King Friday Dec 19 '11 at 18:23
  • @emeraldcode.com: I suppose that you have absolutely another kind of timeout as in the case described in the question. In you case the request come to the server and you have timeout because of long operation on the server. In the question to which I wrote the answer the `status` property of the `jqXHR` object is 0. It's mean the server timeout so that for example the web server is down or you have some other *network error*. In the case the request will be aborted and the status` property of the `jqXHR` object will be set to 0. – Oleg Dec 19 '11 at 19:29
  • 1
    @emeraldcode.com: You are welcome! By the way the behavior with status code 0 is documented in XMLHttpRequest specification on W3: see [here](http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute) and [here](http://www.w3.org/TR/XMLHttpRequest/#error-flag). You can read that in case of "some type of network error or abortion" it will be set so named "error flag" and in the case the `status` attribute of `XMLHttpRequest` will be 0. – Oleg Dec 19 '11 at 21:09
  • cool beans @Oleg. Seems silly that is a standard as an actual error code would be more useful such as 408 etc. Thanks. – King Friday Dec 19 '11 at 21:22
  • Yes, this is how I had implemented error handling, too. It just didn't occur to me that the error callback took a fourth argument that specified the error. The callback method signature is different from local error callbacks. I don't know where this was documented, but thanks for pointing me in the right direction anyway! – Ates Goral Dec 20 '11 at 19:14
  • @AtesGoral: In the example you try to access another web site using full URLs and try use `dataType: 'json'` instead of `dataType: 'jsonp'`. It's not permitted due to browser security restrictions (see [Same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy)). So in the example you have no timeout errors. In [the documentation](http://api.jquery.com/category/ajax/global-ajax-event-handlers/) you can read: "global events are never fired for cross-domain script or JSONP requests". I suppose it's the reason why you has not exception information in the handle. – Oleg Dec 20 '11 at 22:27
  • @Oleg, that example actually works fine thanks to CORS. Chrome attempts the fetch anyway, but then times out due to the shortness of the time out. Thus, I am able to observe the proper "timeout" error reason. – Ates Goral Dec 21 '11 at 00:33
  • 1
    Worth noting where to get statusText : jqXHR.statusText – UpTheCreek Jul 30 '15 at 12:01
  • @UpTheCreek: Sorry, but I don't understand your comment. Do you have some question? Do you found some problems in some specific web browsers or specific jQuery version where `jqXHR.statusText` have another value? – Oleg Jul 30 '15 at 13:32
  • I'm just suggesting that you update your answer to show that statusText is a property of jqXHR. At the moment your answer mentions statusText, but people might not know how to access that. – UpTheCreek Jul 30 '15 at 13:53
  • @UpTheCreek: Thanks for your opinion, but I included [the demo](http://www.ok-soft-gmbh.com/jQuery/AjaxTimeout.htm). Everybody can examine the code and find answers on all opened questions. I don't want to modify the code of answers written 4 year ago. Sorry. – Oleg Jul 30 '15 at 13:59
  • Downvote for unwillingness to include helpful info then. Sorry. (I don't understand what relevance 4 years has) – UpTheCreek Jul 30 '15 at 14:41
  • @UpTheCreek: During the years many question could be just wrong, because many from there have no reference to explicit version of product. So the date of the question is very important. You can see that I posted more as 4500 answers. How many texts could be improved? You can just examine your old answer: [this one](http://stackoverflow.com/a/1200528/315935) on [another one](http://stackoverflow.com/a/1206055/315935). You posted 204 answers. How many your old posts would you rewrite now? I would find many my old answer which I would full rewrite now, but it would get too much time. – Oleg Jul 30 '15 at 16:16
  • @UpTheCreek: **Anonymous users** can *suggest edits* to any post (see [here](http://meta.stackexchange.com/questions/76251/how-do-suggested-edits-work)). The users who have more as 2000 points of reputations can edit other questions imediately. Instead of writing other that he should improve his old text you can do this yourself if you find the changes helpful. The author can review the changes and correct there if required. Such way seems me more productive. – Oleg Jul 30 '15 at 16:16