jQuery AJAX fires error callback on window unload – how do I filter out unload and only catch real errors?

In the error callback or $.ajax you have three input arguments:

function (XMLHttpRequest, textStatus, errorThrown) {
   this; // options for this ajax request
}

You can check directly the xhr.status to get the HTTP response code, for example:

$.ajax({
  url: "test.html",
  cache: false,
  success: function(html){
    $("#results").append(html);
  },
  error: function (xhr, textStatus) {
    if (xhr.status == 500) {
      alert('Server error: '+ textStatus);
    }
  }
});

Edit:
To tell the difference between a connection broken by the browser and the case where the server is down (jasonmerino’s comment):

On unload the xhr.readyState should be 0, where for a non responsive
server the xhr.readyState should be 4.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)