views:

32

answers:

1

I'm trying to handle AJAX errors in a web application that uses jQuery.

I'm setting up an event handler to deal with AJAX errors like this:

$('body').ajaxError(error_handler);

However, despite the information that's passed to that handler, I'm finding it difficult to determine what sort of error occured.

The two errors I'm interested in identifying are:

  • When the application server is down
  • When I've used $.getJSON(...) and the response isn't parsable as JSON data.

Both of these cause my error handler to fire, but I need to handle each error differently, and don't know how I can do this.

Does anyone have any suggestions?

+1  A: 

The XMLHttpRequest is the second argument to the function. You can check the status property to get the HTTP status code. That should tell you if you got a valid response in an unexpected format or if the server is down.

Craig Stuntz