views:

852

answers:

2

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).

+1  A: 

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.

Ryan Lynch
A: 

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.

Cory R. King