jQuery ajax()调用偶尔会导致错误,XmlHttpRequest响应状态为0

时间:2022-10-08 21:15:51

One of the pages in our web application polls the server approximately every 10 seconds for new data, using jQuery's ajax() method. We normally have a few hundred users viewing this page simultaneously, so our server receives several requests per second. The load is not any sort of problem.

我们的Web应用程序中的一个页面使用jQuery的ajax()方法大约每10秒轮询一次服务器以获取新数据。我们通常有几百个用户同时查看此页面,因此我们的服务器每秒会收到多个请求。负载不是任何问题。

For some small percentage of our visitors, the jQuery ajax() call ocassionally triggers it's error event. Most of these error events are obvious errors (such as a timeout when visitors are having network issues), but a small subset of these I'm having a lot of trouble finding the cause of, and I'm a bit perplexed.

对于我们访问者中的一小部分,jQuery ajax()调用偶尔会触发它的错误事件。大多数这些错误事件都是明显的错误(比如访问者遇到网络问题时会超时),但是这些错误的一小部分我找不到原因很麻烦,而且我有点困惑。

We are wiring up a global error listener like so

我们正在连接一个像这样的全局错误监听器

$.ajaxSetup({ 
    timeout: oursettings.timeout,
    error: function(xhr, status, e){
         // error handling goes here
    }
});

For this particular subset of errors, jQuery is throwing us a status of "error". Reading thru the jQuery source code, (specifically line 3574) the error is only thrown with this value when the XmlHttpRequest readystate has a value of 4 (meaning that it is DONE) and the HTTP response status is outside of the success values ( < 200 or >= 300).

对于这个特定的错误子集,jQuery给我们一个“错误”状态。通过jQuery源代码读取(特别是第3574行),当XmlHttpRequest readystate的值为4(意味着它是DONE)并且HTTP响应状态超出成功值(<200)时,只会使用此值抛出错误或> = 300)。

However when we examine the XmlHttpRequest object itself, we find that it has a (HTTP response) status of 0. According to the XHR spec, a status of 0 should be set when the "error flag is true".

但是,当我们检查XmlHttpRequest对象本身时,我们发现它的(HTTP响应)状态为0.根据XHR规范,当“error flag为true”时,应设置状态0。

This is where I am getting really confused though, because the only other thing that the XmlHttpRequest spec states about the "error flag" is

这是我真的很困惑的地方,因为XmlHttpRequest规范中关于“错误标志”的唯一其他事情是

The DONE state has an associated error flag that indicates some type of network error or abortion. It can be either true or false and has an initial value of false.

DONE状态具有相关的错误标志,指示某种类型的网络错误或堕胎。它可以是true或false,初始值为false。

The spec does not state what attribute or where this error flag is kept (i.e., is it available in xhr.error?).

规范没有说明保留这个错误标志的属性或位置(即,它是否在xhr.error中可用?)。

So I guess my question boils down to this: would it be correct for our ajax-error-handling code to assume that any events that hit our error handler with a jQuery status of "error" and a xhr.status of 0 to be caused only by "network error or abortion"?

所以我想我的问题归结为:对于我们的ajax错误处理代码,假设任何触发我们的错误处理程序的事件的jQuery状态为“error”且xhr.status为0都是正确的只有“网络错误或堕胎”?

Is there any other condition or anything that anyone else has seen that could cause the XmlHttpRequest readystate to be DONE (4) with a HTTP response status of 0? I would like to be able to rule out any chance of server-side errors (since our server side logs are not showing anything), and to alter the error-handling-logic to not treat this scenario as a fatal error (which results in the user seeing a bunch of big red alert type error messages).

是否有任何其他条件或任何其他人看到的可能导致XmlHttpRequest readystate为DONE(4)且HTTP响应状态为0的任何其他条件或任何内容?我希望能够排除任何服务器端错误的可能性(因为我们的服务器端日志没有显示任何内容),并且要改变错误处理逻辑以不将此方案视为致命错误(这导致用户看到一堆大红色警报类型错误消息)。

2 个解决方案

#1


4  

My guess would be that it's some sort of issue where the request never makes it to the server so there is no response header with an error code, but the connection is closed properly so the readystate gets a value of 4 i.e. a firewall issue or something like that. Check out this post to see what I mean.

我的猜测是,这是某种问题,其中请求永远不会进入服务器,因此没有带错误代码的响应头,但连接已正确关闭,因此readystate的值为4,即防火墙问题或某事像那样。看看这篇文章,看看我的意思。

#2


3  

We've found this particular status code can be caused by the user navigating away from a page (say, clicking on the back button) before an AJAX call completed.

我们发现这个特定的状态代码可能是由用户在完成AJAX调用之前导航离开页面(例如,单击后退按钮)引起的。

#1


4  

My guess would be that it's some sort of issue where the request never makes it to the server so there is no response header with an error code, but the connection is closed properly so the readystate gets a value of 4 i.e. a firewall issue or something like that. Check out this post to see what I mean.

我的猜测是,这是某种问题,其中请求永远不会进入服务器,因此没有带错误代码的响应头,但连接已正确关闭,因此readystate的值为4,即防火墙问题或某事像那样。看看这篇文章,看看我的意思。

#2


3  

We've found this particular status code can be caused by the user navigating away from a page (say, clicking on the back button) before an AJAX call completed.

我们发现这个特定的状态代码可能是由用户在完成AJAX调用之前导航离开页面(例如,单击后退按钮)引起的。