在JQuery ajax中,如何正确处理HTTP 408错误?

时间:2022-11-29 14:07:55

I do a JQuery Ajax call with the "error" option set. In my "error" method I would like to handle an HTTP 408 differently from a normal HTTP 500.

我使用“error”选项集进行JQuery Ajax调用。在我的“错误”方法中,我想处理HTTP 408与普通HTTP 500不同。

The problem is that the jxhr.statusCode is 0 and the "status" value is simply "error"! But in firebug I can see that the service definitely returned status code 408.

问题是jxhr.statusCode为0,“status”值只是“错误”!但在firebug中我可以看到该服务肯定返回状态码408。

Why would the jxhr.statusCode be 0? Is there another way to identify an HTTP 408 error?

为什么jxhr.statusCode为0?有没有其他方法来识别HTTP 408错误?

My error handler code looks as follows (for a 408 error the code just continues into the 'else' block):

我的错误处理程序代码如下所示(对于408错误,代码只是继续进入'else'块):

error: function (jxhr, status, error) {
var $message = element.next().find('.loadmask-msg-label');
if (jxhr.status == 401)
  $message.html(settings.unauthorizedMessage);
else if (jxhr.status == 408)
  $message.html(settings.timeoutMessage);
else
  $message.html(settings.errorMessage);

2 个解决方案

#1


3  

According to Jquery you should do this :

根据Jquery你应该这样做:

在JQuery ajax中,如何正确处理HTTP 408错误?

$.ajax({
  statusCode: {
    404: function() {
      alert('page not found');
    }
  }
})

;

;

#2


3  

The problem was FIREFOX. In any browser except Firefox the status code is 408. In Firefox the status code is 0, and no response headers are returned. Also, Firefox seems to re-request 10 times because of the 408 response, I have no idea why! And this is only for an HTTP 408 response.

问题是FIREFOX。在除Firefox之外的任何浏览器中,状态代码为408.在Firefox中,状态代码为0,并且不返回任何响应头。此外,由于408响应,Firefox似乎重新请求了10次,我不明白为什么!这仅适用于HTTP 408响应。

In the end we had to return a HTTP 500 error with custom headers.

最后,我们不得不返回带有自定义标头的HTTP 500错误。

Dirty :(

脏:(

#1


3  

According to Jquery you should do this :

根据Jquery你应该这样做:

在JQuery ajax中,如何正确处理HTTP 408错误?

$.ajax({
  statusCode: {
    404: function() {
      alert('page not found');
    }
  }
})

;

;

#2


3  

The problem was FIREFOX. In any browser except Firefox the status code is 408. In Firefox the status code is 0, and no response headers are returned. Also, Firefox seems to re-request 10 times because of the 408 response, I have no idea why! And this is only for an HTTP 408 response.

问题是FIREFOX。在除Firefox之外的任何浏览器中,状态代码为408.在Firefox中,状态代码为0,并且不返回任何响应头。此外,由于408响应,Firefox似乎重新请求了10次,我不明白为什么!这仅适用于HTTP 408响应。

In the end we had to return a HTTP 500 error with custom headers.

最后,我们不得不返回带有自定义标头的HTTP 500错误。

Dirty :(

脏:(