It appears that when you make an XMLHttpRequest from a script in a browser, if the browser is set to work offline or if the network cable is pulled out, the request completes with an error and with status = 0. 0 is not listed among permissible HTTP status codes.
当您从浏览器中的脚本创建XMLHttpRequest时,如果浏览器被设置为脱机工作,或者网线被拉出,请求将以一个错误和状态= 0完成。0不在允许的HTTP状态代码中列出。
What does a status code of 0 mean? Does it mean the same thing across all browsers, and for all HTTP client utilities? Is it part of the HTTP spec or is it part of some other protocol spec? It seems to mean that the HTTP request could not be made at all, perhaps because the server address could not be resolved.
0的状态码是什么意思?它在所有浏览器和所有HTTP客户端实用程序中是否意味着相同的事情?它是HTTP规范的一部分还是其他协议规范的一部分?这似乎意味着根本无法发出HTTP请求,可能是因为服务器地址无法解析。
What error message is appropriate to show the user? "Either you are not connected to the internet, or the website is encountering problems, or there might be a typing error in the address"?
什么错误消息适合显示给用户?“或者你没有连接到互联网,或者网站遇到了问题,或者地址中可能有输入错误”?
I should add to this that I see the behavior in FireFox when set to "Work Offline", but not in Microsoft Internet Explorer when set to "Work Offline". In IE, the user gets a dialog giving the option to go online. FireFox does not notify the user before returning the error.
我应该补充一点,我在FireFox中看到了“离线工作”时的行为,但在“离线工作”时,却不在微软Internet Explorer中。在IE中,用户会得到一个对话框,提供上网选项。FireFox在返回错误之前不会通知用户。
I am asking this in response to a request to "show a better error message". What Internet Explorer does is good. It tells the user what is causing the problem and gives them the option to fix it. In order to give an equivalent UX with FireFox I need to infer the cause of the problem and inform the user. So what in total can I infer from Status 0? Does it have a universal meaning or does it tell me nothing?
我问这个问题是为了回应“显示一个更好的错误消息”的请求。ie做的很好。它告诉用户造成问题的原因,并给予他们解决问题的选项。为了给FireFox提供一个等价的用户体验,我需要推断问题的原因并通知用户。从状态0可以推断出什么?它有一个普遍的意义还是什么都不告诉我?
6 个解决方案
#1
129
Short Answer
It's not a HTTP response code, but it is documented by W3 as a valid value for the status
attribute of an XMLHttpRequest
(and thus also of a jqXHR
object, for jQuery users).
它不是HTTP响应代码,但是W3将它记录为XMLHttpRequest的状态属性的有效值(因此,对于jQuery用户,也是jqXHR对象的有效值)。
It covers a whole swathe of possible situations in which there's no real HTTP response code available to report, either because you haven't sent the request, you explicitly aborted it, the page is unloading, or x went wrong for one of many possible values of x.
它涵盖了所有可能的情况,其中没有可用的真正HTTP响应代码可以报告,或者因为您没有发送请求,显式地中止请求,页面正在卸载,或者x在许多可能的x值中出现错误。
Long Answer
First, to reiterate: 0 is not a HTTP status code. There's a complete list of them in RFC 7231 Section 6.1, that doesn't include 0, and the intro to section 6 states clearly that
首先,重申一下:0不是HTTP状态码。在RFC 7231节6.1中有一个完整的列表,它不包括0,第6节的介绍清楚地说明了这一点
The status-code element is a three-digit integer code
状态代码元素是一个三位数的整数代码
which 0 is not.
0是不。
However, 0 as a value of the status
attribute of an XMLHttpRequest
object is documented. From documentation at http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute:
但是,XMLHttpRequest对象的状态属性的值被记录下来。从文档:http://www.w3.org/TR/XMLHttpRequest/ # status属性:
4.7.1 The status attribute
The
status
attribute must return the result of running these steps:status属性必须返回运行这些步骤的结果:
If the state is UNSENT or OPENED, return 0.
如果状态未发送或打开,返回0。
If the error flag is set, return 0.
如果设置了错误标志,则返回0。
Return the HTTP status code.
返回HTTP状态码。
We can dig deeper into the spec and find out just what those conditions for returning 0 mean. From http://www.w3.org/TR/XMLHttpRequest/#states:
我们可以深入研究规范,找出返回0的条件意味着什么。从http://www.w3.org/TR/XMLHttpRequest/状态:
4.5 States
...
…
UNSENT
(numeric value 0)The object has been constructed.
对象已经构造好了。
OPENED
(numeric value 1)The
open()
method has been successfully invoked. During this state request headers can be set usingsetRequestHeader()
and the request can be made using thesend()
method.open()方法已被成功调用。在此状态下,可以使用setRequestHeader()设置请求头,并且可以使用send()方法进行请求。
...
…
The error flag indicates some type of network error or fetch termination. It is initially unset.
错误标志指示某种类型的网络错误或获取终止。这是最初设置。
It's also relevant to note that the next possible state after UNSENT
and OPENED
is HEADERS_RECEIVED
:
还需要注意的是,未发送和打开后的下一个可能状态是HEADERS_RECEIVED:
HEADERS_RECEIVED
(numeric value 2)All redirects (if any) have been followed and all HTTP headers of the final response have been received. Several response members of the object are now available.
所有重定向(如果有的话)都被跟踪,最终响应的所有HTTP头都被接收。对象的几个响应成员现在是可用的。
Putting this all together, the short answer is that 0 is simply what gets returned by the status
attribute of an XMLHttpRequest
object when there is no real status code to return, because either:
综上所述,简短的答案是,0仅仅是XMLHttpRequest对象的状态属性在没有真正的状态代码返回时返回的值,因为:
- The request hasn't yet been sent, or
- 请求还没有发送
- The request has been sent but the headers of the response have not yet been received, or
- 已经发送了请求,但是还没有收到响应的头部,或者
- One of many possible circumstances have occurred, listed in the docs, that have caused the "error flag" to be set.
- 在文档中列出的许多可能的情况之一导致了“错误标志”的设置。
Okay, but what errors can cause this mysterious "error flag" to be set? If you CTRL-F for 'error flag' in the W3 documentation, you will find that it gets unset upon sending the request, and it only ever gets set as part of the algorithm to "terminate the request". Looking for all the places that algorithm is invoked, you'll find that it happens when:
好的,但是什么错误可以导致这个神秘的“错误标志”被设置?如果您在W3文档中按下CTRL-F表示“错误标志”,您将发现它在发送请求时被取消设置,并且它只被设置为“终止请求”算法的一部分。查找该算法被调用的所有位置,您会发现:
- The request is opened (or re-opened) with the
open()
method - 使用open()方法打开(或重新打开)请求
- The request is garbage collected (e.g. when leaving the page)
- 请求是垃圾收集(例如,离开页面时)
- The request is aborted with the
abort()
method - 使用abort()方法中止请求
-
A 'request error' happens, which can happen when one of the following situations occurs:
发生“请求错误”,可能发生以下情况之一:
-
A network error occurs, which can happen if
发生网络错误,如果发生的话
- There's an infinite redirect loop
- 有一个无限重定向循环
- There is/are
DNS errors, TLS negotiation failure, or other type of network error
DNS错误、TLS协商失败或其他类型的网络错误
- The request was a CORS request, and the response cannot be shared
- 请求是CORS请求,不能共享响应
-
An abort error occurs, which can only happen if
发生中止错误,只有当
The end user cancels the request
最终用户取消请求
whatever that means. I don't know of any browser that shows users when AJAX requests are occurring and gives them the opportunity to cancel them explicitly, so I think this one is - at least today - irrelevant.
不管这意味着什么。我不知道有哪个浏览器会在AJAX请求发生时显示给用户,并让他们有机会显式地取消它们,所以我认为这个浏览器——至少在今天——是不相关的。
-
A timeout error occurs, which means, reasonably enough, that
发生超时错误,这意味着,足够合理地
timeout
is not 0 and since the request started the amount of milliseconds specified bytimeout
has passed超时不是0,并且自从请求启动以来,超时指定的毫秒数已被传递
-
As far as XMLHttpRequest
goes, that's everything.
就XMLHttpRequest而言,这就是一切。
Beyond XMLHttpRequest
, I would speculate that HTTP libraries in languages outside of JavaScript may well be using a 0 status code similarly, as a default value when no status code has been received from the server.
除了XMLHttpRequest之外,我还推测在JavaScript之外的语言中,HTTP库很可能同样使用0状态码作为默认值,当服务器没有收到状态码时。
#2
51
status 0 appear when an ajax call was cancelled before getting the response by refreshing the page or requesting a URL that is unreachable.
当ajax调用在刷新页面或请求不可访问的URL之前被取消时,状态0将出现。
this status is not documented but exist over ajax and makeRequest call's from gadget.io.
此状态没有文档记录,但是存在于ajax和makeRequest调用的gadget.io上。
#3
4
from documentation http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute means a request was cancelled before going anywhere
从文档http://www.w3.org/TR/XMLHttpRequest/# status-attribute可以看出,一个请求在去任何地方之前都被取消了
#4
3
Know it's an old post. But these issues still exist.
知道这是一个老帖子。但这些问题仍然存在。
Here are some of my findings on the subject, grossly explained.
以下是我在这个问题上的一些发现。
"Status" 0 means one of 3 things, as per the XMLHttpRequest spec:
“Status”0表示三种情况之一,如XMLHttpRequest规范所示:
-
dns name resolution failed (that's for instance when network plug is pulled out)
dns名称解析失败(例如,当拔出网络插头时)
-
server did not answer (a.k.a. unreachable or unresponding)
服务器没有应答(即无法或无法应答)
-
request was aborted because of a CORS issue (abortion is performed by the user-agent and follows a failing OPTIONS pre-flight).
由于CORS问题,请求被中止(由用户代理执行人工流产,并遵循失败的选项)。
If you want to go further, dive deep into the inners of XMLHttpRequest. I suggest reading the ready-state update sequence ([0,1,2,3,4] is the normal sequence, [0,1,4] corresponds to status 0, [0,1,2,4] means no content sent which may be an error or not). You may also want to attach listeners to the xhr (onreadystatechange, onabort, onerror, ontimeout) to figure out details.
如果您想更进一步,请深入了解XMLHttpRequest的输入。我建议读取已经状态更新序列([0,1,2,3,4]是正常序列,[0,1,4]对应状态0,[0,1,2,4]表示没有发送可能是错误的内容)。您可能还希望将侦听器连接到xhr (onreadystatechange、onabort、onerror、ontimeout)来计算细节。
From the spec (XHR Living spec):
从规格(XHR生活规格):
const unsigned short UNSENT = 0;
const unsigned short OPENED = 1;
const unsigned short HEADERS_RECEIVED = 2;
const unsigned short LOADING = 3;
const unsigned short DONE = 4;
#5
0
Since iOS 9, you need to add "App Transport Security Settings" to your info.plist file and allow "Allow Arbitrary Loads" before making request to non-secure HTTP web service. I had this issue in one of my app.
自从ios9以来,你需要在你的信息中添加“应用程序传输安全设置”。plist文件,允许在请求非安全的HTTP web服务之前“允许任意加载”。在我的一个应用程序中有这个问题。
#6
-1
Yes, some how the ajax call aborted. The cause may be following.
是的,一些ajax调用是如何终止的。原因可能是这样的。
- Before completion of ajax request, user navigated to other page.
- 在完成ajax请求之前,用户导航到其他页面。
- Ajax request have timeout.
- Ajax请求超时。
- Server is not able to return any response.
- 服务器无法返回任何响应。
#1
129
Short Answer
It's not a HTTP response code, but it is documented by W3 as a valid value for the status
attribute of an XMLHttpRequest
(and thus also of a jqXHR
object, for jQuery users).
它不是HTTP响应代码,但是W3将它记录为XMLHttpRequest的状态属性的有效值(因此,对于jQuery用户,也是jqXHR对象的有效值)。
It covers a whole swathe of possible situations in which there's no real HTTP response code available to report, either because you haven't sent the request, you explicitly aborted it, the page is unloading, or x went wrong for one of many possible values of x.
它涵盖了所有可能的情况,其中没有可用的真正HTTP响应代码可以报告,或者因为您没有发送请求,显式地中止请求,页面正在卸载,或者x在许多可能的x值中出现错误。
Long Answer
First, to reiterate: 0 is not a HTTP status code. There's a complete list of them in RFC 7231 Section 6.1, that doesn't include 0, and the intro to section 6 states clearly that
首先,重申一下:0不是HTTP状态码。在RFC 7231节6.1中有一个完整的列表,它不包括0,第6节的介绍清楚地说明了这一点
The status-code element is a three-digit integer code
状态代码元素是一个三位数的整数代码
which 0 is not.
0是不。
However, 0 as a value of the status
attribute of an XMLHttpRequest
object is documented. From documentation at http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute:
但是,XMLHttpRequest对象的状态属性的值被记录下来。从文档:http://www.w3.org/TR/XMLHttpRequest/ # status属性:
4.7.1 The status attribute
The
status
attribute must return the result of running these steps:status属性必须返回运行这些步骤的结果:
If the state is UNSENT or OPENED, return 0.
如果状态未发送或打开,返回0。
If the error flag is set, return 0.
如果设置了错误标志,则返回0。
Return the HTTP status code.
返回HTTP状态码。
We can dig deeper into the spec and find out just what those conditions for returning 0 mean. From http://www.w3.org/TR/XMLHttpRequest/#states:
我们可以深入研究规范,找出返回0的条件意味着什么。从http://www.w3.org/TR/XMLHttpRequest/状态:
4.5 States
...
…
UNSENT
(numeric value 0)The object has been constructed.
对象已经构造好了。
OPENED
(numeric value 1)The
open()
method has been successfully invoked. During this state request headers can be set usingsetRequestHeader()
and the request can be made using thesend()
method.open()方法已被成功调用。在此状态下,可以使用setRequestHeader()设置请求头,并且可以使用send()方法进行请求。
...
…
The error flag indicates some type of network error or fetch termination. It is initially unset.
错误标志指示某种类型的网络错误或获取终止。这是最初设置。
It's also relevant to note that the next possible state after UNSENT
and OPENED
is HEADERS_RECEIVED
:
还需要注意的是,未发送和打开后的下一个可能状态是HEADERS_RECEIVED:
HEADERS_RECEIVED
(numeric value 2)All redirects (if any) have been followed and all HTTP headers of the final response have been received. Several response members of the object are now available.
所有重定向(如果有的话)都被跟踪,最终响应的所有HTTP头都被接收。对象的几个响应成员现在是可用的。
Putting this all together, the short answer is that 0 is simply what gets returned by the status
attribute of an XMLHttpRequest
object when there is no real status code to return, because either:
综上所述,简短的答案是,0仅仅是XMLHttpRequest对象的状态属性在没有真正的状态代码返回时返回的值,因为:
- The request hasn't yet been sent, or
- 请求还没有发送
- The request has been sent but the headers of the response have not yet been received, or
- 已经发送了请求,但是还没有收到响应的头部,或者
- One of many possible circumstances have occurred, listed in the docs, that have caused the "error flag" to be set.
- 在文档中列出的许多可能的情况之一导致了“错误标志”的设置。
Okay, but what errors can cause this mysterious "error flag" to be set? If you CTRL-F for 'error flag' in the W3 documentation, you will find that it gets unset upon sending the request, and it only ever gets set as part of the algorithm to "terminate the request". Looking for all the places that algorithm is invoked, you'll find that it happens when:
好的,但是什么错误可以导致这个神秘的“错误标志”被设置?如果您在W3文档中按下CTRL-F表示“错误标志”,您将发现它在发送请求时被取消设置,并且它只被设置为“终止请求”算法的一部分。查找该算法被调用的所有位置,您会发现:
- The request is opened (or re-opened) with the
open()
method - 使用open()方法打开(或重新打开)请求
- The request is garbage collected (e.g. when leaving the page)
- 请求是垃圾收集(例如,离开页面时)
- The request is aborted with the
abort()
method - 使用abort()方法中止请求
-
A 'request error' happens, which can happen when one of the following situations occurs:
发生“请求错误”,可能发生以下情况之一:
-
A network error occurs, which can happen if
发生网络错误,如果发生的话
- There's an infinite redirect loop
- 有一个无限重定向循环
- There is/are
DNS errors, TLS negotiation failure, or other type of network error
DNS错误、TLS协商失败或其他类型的网络错误
- The request was a CORS request, and the response cannot be shared
- 请求是CORS请求,不能共享响应
-
An abort error occurs, which can only happen if
发生中止错误,只有当
The end user cancels the request
最终用户取消请求
whatever that means. I don't know of any browser that shows users when AJAX requests are occurring and gives them the opportunity to cancel them explicitly, so I think this one is - at least today - irrelevant.
不管这意味着什么。我不知道有哪个浏览器会在AJAX请求发生时显示给用户,并让他们有机会显式地取消它们,所以我认为这个浏览器——至少在今天——是不相关的。
-
A timeout error occurs, which means, reasonably enough, that
发生超时错误,这意味着,足够合理地
timeout
is not 0 and since the request started the amount of milliseconds specified bytimeout
has passed超时不是0,并且自从请求启动以来,超时指定的毫秒数已被传递
-
As far as XMLHttpRequest
goes, that's everything.
就XMLHttpRequest而言,这就是一切。
Beyond XMLHttpRequest
, I would speculate that HTTP libraries in languages outside of JavaScript may well be using a 0 status code similarly, as a default value when no status code has been received from the server.
除了XMLHttpRequest之外,我还推测在JavaScript之外的语言中,HTTP库很可能同样使用0状态码作为默认值,当服务器没有收到状态码时。
#2
51
status 0 appear when an ajax call was cancelled before getting the response by refreshing the page or requesting a URL that is unreachable.
当ajax调用在刷新页面或请求不可访问的URL之前被取消时,状态0将出现。
this status is not documented but exist over ajax and makeRequest call's from gadget.io.
此状态没有文档记录,但是存在于ajax和makeRequest调用的gadget.io上。
#3
4
from documentation http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute means a request was cancelled before going anywhere
从文档http://www.w3.org/TR/XMLHttpRequest/# status-attribute可以看出,一个请求在去任何地方之前都被取消了
#4
3
Know it's an old post. But these issues still exist.
知道这是一个老帖子。但这些问题仍然存在。
Here are some of my findings on the subject, grossly explained.
以下是我在这个问题上的一些发现。
"Status" 0 means one of 3 things, as per the XMLHttpRequest spec:
“Status”0表示三种情况之一,如XMLHttpRequest规范所示:
-
dns name resolution failed (that's for instance when network plug is pulled out)
dns名称解析失败(例如,当拔出网络插头时)
-
server did not answer (a.k.a. unreachable or unresponding)
服务器没有应答(即无法或无法应答)
-
request was aborted because of a CORS issue (abortion is performed by the user-agent and follows a failing OPTIONS pre-flight).
由于CORS问题,请求被中止(由用户代理执行人工流产,并遵循失败的选项)。
If you want to go further, dive deep into the inners of XMLHttpRequest. I suggest reading the ready-state update sequence ([0,1,2,3,4] is the normal sequence, [0,1,4] corresponds to status 0, [0,1,2,4] means no content sent which may be an error or not). You may also want to attach listeners to the xhr (onreadystatechange, onabort, onerror, ontimeout) to figure out details.
如果您想更进一步,请深入了解XMLHttpRequest的输入。我建议读取已经状态更新序列([0,1,2,3,4]是正常序列,[0,1,4]对应状态0,[0,1,2,4]表示没有发送可能是错误的内容)。您可能还希望将侦听器连接到xhr (onreadystatechange、onabort、onerror、ontimeout)来计算细节。
From the spec (XHR Living spec):
从规格(XHR生活规格):
const unsigned short UNSENT = 0;
const unsigned short OPENED = 1;
const unsigned short HEADERS_RECEIVED = 2;
const unsigned short LOADING = 3;
const unsigned short DONE = 4;
#5
0
Since iOS 9, you need to add "App Transport Security Settings" to your info.plist file and allow "Allow Arbitrary Loads" before making request to non-secure HTTP web service. I had this issue in one of my app.
自从ios9以来,你需要在你的信息中添加“应用程序传输安全设置”。plist文件,允许在请求非安全的HTTP web服务之前“允许任意加载”。在我的一个应用程序中有这个问题。
#6
-1
Yes, some how the ajax call aborted. The cause may be following.
是的,一些ajax调用是如何终止的。原因可能是这样的。
- Before completion of ajax request, user navigated to other page.
- 在完成ajax请求之前,用户导航到其他页面。
- Ajax request have timeout.
- Ajax请求超时。
- Server is not able to return any response.
- 服务器无法返回任何响应。