The AJAX Errors That No-one Else Talks About

Posted on August 16th, 2006 by tom.
Categories: Tech.

Handling AJAX errors is about more than just checking the readyState, status and statusText.

The W3 working draft for the XMLHttpRequest object states that when the readyState of an XMLHttpRequest is 3 or 4, then status and statusText must be available. These values can then be used to determine if the request was successful or not. By and large this is true, you will see a status of 200 when everything is OK and something like 404 when you make a mistake. What isn't specified however is what happens if the network connection goes down while the XMLHttpRequest is in flight or is down when the XMLHttpRequest is sent.

The following table details the values obtained or the behaviour observed in various browsers when an XMLHttpRequest fails due to a network failure.

Browser Version OS readyState status statusText
Firefox 1.5.0.6 WinXP SP2 4 exception thrown exception thrown
Internet Explorer 6.0.2900 WinXP SP2 4 12029 Unknown
Opera 9.01 WinXP SP2 4 0 empty
Safari 2.0.4 OS X 10.4.6 4 undefined undefined

Obviously this list is very far from complete. If you know the behaviour of other browsers or other browser versions, please let me know (via the comment section below) and I'll update the list accordingly. To run the test yourself, you can use the Event Driven AJAX Demonstration and

  1. Turn debugging on
  2. Click "Lighten Page"
  3. You should see "getEvents waiting.." in the debug window
  4. Sever your network connection (or if you are running the demo locally, terminate the web server)
  5. record the "readyState", "status" and "statusText" reported in the debug window.

To create your own test:

  1. Create a page that sends and AJAX request when a user clicks a link or button
  2. Load the page
  3. Sever your network connection (or if you are running the test locally, terminate the web server)
  4. Click on the link or button to send the request
  5. record the values of "readyState", "status" and "statusText" or record if an exception is thrown trying to access the values.

Admittedly, XMLHttpRequests rarely fail due to network issues, but it is nice to be able to detect when this occurs and handle it gracefully with your client side code. Probably the safest way to handle this type of error is to display a message in an appropriate place on your page. If your error message widget uses images you should try to pre-load these when the page loads (if possible), because the chances are that if the XMLHttpRequest failed, then an image request will fail too. For the same reason you should avoid redirecting the user to another page, because the chances are they won't be able to reach that either.


Technorati : , ,

4 comments.

  1. balkrishna
    Comment on March 28th, 2007. Reply

    hi all i got an exception for ajax for denite call to xmlhttpRequest.open method can you have any suggestions for this

  2. Comment on March 28th, 2007. Reply

    Hi balkrishna,

    You are going to have to provide a few more details. What browser are you using? what is ‘denite’? what is the exception? what is the value of readyState, status and statusText?

  3. Comment on January 9th, 2008. Reply

    OK… sorry for my english… so isn’t possible to fixe this error… or prevent happened…????

  4. Comment on January 11th, 2008. Reply

    No you can prevent this error (how could you guarantee that the network connection to the web server doesn’t go down). The only thing you can do is handle it gracefully via some cleverly crafted JavaScript.

Leave a comment

Names and email addresses are required (email addresses aren't displayed), url's are optional.

Comments may contain the following xhtml tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>