English 中文(简体)
jQuery ajax() call occasionally results in error with XmlHttpRequest response status of 0
原标题:

One of the pages in our web application polls the server approximately every 10 seconds for new data, using jQuery s ajax() method. We normally have a few hundred users viewing this page simultaneously, so our server receives several requests per second. The load is not any sort of problem.

For some small percentage of our visitors, the jQuery ajax() call ocassionally triggers it s error event. Most of these error events are obvious errors (such as a timeout when visitors are having network issues), but a small subset of these I m having a lot of trouble finding the cause of, and I m a bit perplexed.

We are wiring up a global error listener like so

$.ajaxSetup({ 
    timeout: oursettings.timeout,
    error: function(xhr, status, e){
         // error handling goes here
    }
});

For this particular subset of errors, jQuery is throwing us a status of "error". Reading thru the jQuery source code, (specifically line 3574) the error is only thrown with this value when the XmlHttpRequest readystate has a value of 4 (meaning that it is DONE) and the HTTP response status is outside of the success values ( < 200 or >= 300).

However when we examine the XmlHttpRequest object itself, we find that it has a (HTTP response) status of 0. According to the XHR spec, a status of 0 should be set when the "error flag is true".

This is where I am getting really confused though, because the only other thing that the XmlHttpRequest spec states about the "error flag" is

The DONE state has an associated error flag that indicates some type of network error or abortion. It can be either true or false and has an initial value of false.

The spec does not state what attribute or where this error flag is kept (i.e., is it available in xhr.error?).

So I guess my question boils down to this: would it be correct for our ajax-error-handling code to assume that any events that hit our error handler with a jQuery status of "error" and a xhr.status of 0 to be caused only by "network error or abortion"?

Is there any other condition or anything that anyone else has seen that could cause the XmlHttpRequest readystate to be DONE (4) with a HTTP response status of 0? I would like to be able to rule out any chance of server-side errors (since our server side logs are not showing anything), and to alter the error-handling-logic to not treat this scenario as a fatal error (which results in the user seeing a bunch of big red alert type error messages).

最佳回答

My guess would be that it s some sort of issue where the request never makes it to the server so there is no response header with an error code, but the connection is closed properly so the readystate gets a value of 4 i.e. a firewall issue or something like that. Check out this post to see what I mean.

问题回答

We ve found this particular status code can be caused by the user navigating away from a page (say, clicking on the back button) before an AJAX call completed.





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签