Domanda

I'm trying to detect when an Ajax (xhr) request gets a status code of 408 in order to direct the user to a login/timeout page.

At the moment I'm using an old version of jQuery (1.3.2) to perform all of my ajax requests and have the following bit of code to detect 408 error responses:

$().ready(function() {
  $.ajaxSetup({
    error:function(x,e) {
      if (x.status==408) {
        document.location.href = '/timeout.html'; 
      } else {
        document.location.href = '/error.html'; 
      }   
    }
  });
});

This code is not working as intended in Firefox and the xhr object has a status code of 0 and empty status text even though the status code is indeed 408. As a result the general error page is loaded instead of the timeout page.

EDIT: I tried console.dir(x) and got this output (as you can see status is 0 and status text is ""): enter image description here

The ajax request is not cross domain and is being made from the same domain.

I also noticed that all response headers are cleared for any XHR that has an error status in Firefox.

None of these issues are evident in Chrome.

I even tried a simple XMLHttpRequest to a URL that will return a 408 status (all on the same domain/origin) and all headers were null - example shown below:

var x = new XMLHttpRequest();
x.open("GET", "http://example.com/url-that-will-return-408-status");
x.onreadystatechange = function() {
  if(this.readyState == 2) {
    console.log('CustomErrorCode=' + this.getResponseHeader('CustomErrorCode'));
    console.log('Content-Length=' + this.getResponseHeader('Content-Length'));
    console.log('Content-Type=' + this.getResponseHeader('Content-Type'));
    console.log(this.getAllResponseHeaders());
  }
}
x.send();

Any ideas as to why Firefox is different to Chrome in this regard and how I can work around this issue? Based on my reading of the W3C spec it seems that maybe Firefox is doing the right thing but I don't quite understand why response headers would be cleared for an error status.

È stato utile?

Soluzione

Check this out.

It looks like Firefox re-requests 10 times as long as a 408 status code is provided.

A ticket has been opened about this Firefox "issue", and this behaviour is actually not compliant with the RFC. Since the ticked is officially marked as RESOLVED FIXED (as many other 408-related issues, here and here), I would suggest you to check your version and eventually re-open it, providing further information about your environment.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top