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?

1 Answer 1

9
+50

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.

Sign up to request clarification or add additional context in comments.

18 Comments

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.
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.
@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.
@emeraldcode.com: You are welcome! By the way the behavior with status code 0 is documented in XMLHttpRequest specification on W3: see here and here. 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.
Worth noting where to get statusText : jqXHR.statusText
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.