Question: If I navigate away from a page in the middle of an $.ajax() request it fires the error callback. I've tested in Safari and FF with both GET and POST requests.

One potential solution would be to abort all AJAX requests on page unload, but the error handler is called before unload, so this doesn't seem possible.

I want to be able to handle REAL errors such as 500s gracefully on the client side with a polite alert or a modal dialog, but I don't want this handling to be called when a user navigates away from the page.

How do I do this?

--

(Also strange: When navigating away from a page, the error handler says that the textStatus parameter is "error", the same it throws when receiving a 500/bad request.)

Answer: 
*(I'd add this as a comment to the main answer but haven't built up enough points to do so yet!)*

I'm also seeing this in FF4 and Chrome (9.0.597.107). Probably elsewhere but that's bad enough for me to want to fix it!

One of the things that's odd about this situation is that returned XMLHttpRequest.status === 0

Which seems like a reliable way to detect this situation and, in my particular case, abort the custom error handling that displays to the user:

```
error: function (XMLHttpRequest, textStatus, errorThrown) {
    if (XMLHttpRequest.status === 0) return;
    // error handling here
}

```

Also worth mentioning that on the assumption if may be a problem in the JSON parse of whatever the browser is giving back to the $.ajax() call, I also tried swapping out the native JSON.stringify for the Douglas Crockford version ( <https://github.com/douglascrockford/JSON-js> ) but that made no difference.
If you make use of jQuery functions such as `$.get`, `$.post`, `$.ajax`... then you can use the parameter `text_status` to check what type of fail is:

```
request = $.get('test.html', function(data){
    //whatever
}).fail(function(xhr, text_status, error_thrown) {

    if(text_status!== 'abort'){
        console.warn("Error!!");
    }
});

```

From the [jQuery docs](http://api.jquery.com/jquery.ajax/):

> 
> **jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});** An alternative construct to the error callback option, the .fail() method
>  replaces the deprecated .error() method. Refer to deferred.fail() for
>  implementation details.
> 
> 
>