ajax HTTP 请求返回状态码 0 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/872206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does it mean when an HTTP request returns status code 0?
提问by mike nelson
What does it mean when JavaScript network calls such as fetch or XMLHttpRequest, or any other type of HTTP network request, fail with an HTTP status code of 0?
当 JavaScript 网络调用(例如 fetch 或 XMLHttpRequest,或任何其他类型的 HTTP 网络请求)失败并显示 HTTP 状态代码 0 时,这意味着什么?
This doesn't seem to be a valid HTTP status code as other codes are three digits in HTTP specification.
这似乎不是有效的 HTTP 状态代码,因为其他代码是 HTTP 规范中的三位数字。
I tried unplugging the network completely as a test. It may be unrelated, but that resulted in status code 17003 (IIRC), which cursory searching suggests means "DNS server lookup failed".
我尝试完全拔掉网络作为测试。它可能不相关,但这导致状态代码 17003 (IIRC),粗略搜索表明这意味着“DNS 服务器查找失败”。
The same code works fine from some locations and systems, however within certain environments it fails with status code 0 and there is no responseText provided.
相同的代码在某些位置和系统上运行良好,但是在某些环境中它会失败并显示状态代码 0 并且没有提供 responseText。
This is a typical HTTP POST to an Internet URL. It does not involve file:// which I understand may return 0 indicating success in Firefox.
这是对 Internet URL 的典型 HTTP POST。它不涉及 file:// ,据我所知可能返回 0 表示在 Firefox 中成功。
采纳答案by Nick
I believe the error code indicates that the response was empty, (as not even headers were returned). This means the connection was accepted and then closed gracefully (TCP FIN). There are a number of things which could cause this, but based off of your description, some form of firewall seems the most likely culprit.
我相信错误代码表明响应为空(因为甚至没有返回标头)。这意味着连接被接受,然后正常关闭(TCP FIN)。导致这种情况的原因有很多,但根据您的描述,某种形式的防火墙似乎是最有可能的罪魁祸首。
回答by whitneyland
Many of the answers here are wrong.It seems people figure out what was causing status==0 in their particular case and then generalize that as the answer.
这里的许多答案都是错误的。人们似乎弄清楚了在他们的特定情况下导致 status==0 的原因,然后将其概括为答案。
Practically speaking, status==0 for a failed XmlHttpRequest should be considered an undefined error.
实际上,失败的 XmlHttpRequest 的 status==0 应该被视为未定义的错误。
The actual W3C spec defines the conditions for which zero is returned here: https://fetch.spec.whatwg.org/#concept-network-error
实际的 W3C 规范定义了在此处返回零的条件:https: //fetch.spec.whatwg.org/#concept-network-error
As you can see from the spec (fetch or XmlHttpRequest) this code could be the result of an error that happened even before the server is contacted.
正如您从规范(fetch 或 XmlHttpRequest)中看到的,此代码可能是在联系服务器之前发生的错误的结果。
Some of the common situations that produce this status code are reflected in the other answers but it could be any or none of these problems:
产生此状态代码的一些常见情况反映在其他答案中,但可能是这些问题中的任何一个或都不是:
- Illegal cross origin request (see CORS)
- Firewall block or filtering
- The request itself was cancelled in code
- An installed browser extension is mucking things up
- 非法跨源请求(参见CORS)
- 防火墙阻止或过滤
- 请求本身在代码中被取消
- 已安装的浏览器扩展程序正在搞砸
What would be helpful would be for browsers to provide detailed error reporting for more of these status==0 scenarios. Indeed, sometimes status==0 will accompany a helpful console message, but in others there is no other information.
浏览器为更多这些 status==0 场景提供详细的错误报告是有帮助的。事实上,有时 status==0 会伴随有用的控制台消息,但在其他情况下没有其他信息。
回答by Cory R. King
For what it is worth, depending on the browser, jQuery-based AJAX calls will call your success callback with a HTTP status code of 0. We've found a status code of "0" usually means the user navigated to a different page before the AJAX call completed.
对于它的价值,取决于浏览器,基于 jQuery 的 AJAX 调用将使用 HTTP 状态代码 0 调用您的成功回调。我们发现状态代码“0”通常意味着用户之前导航到不同的页面AJAX 调用完成。
Not the same technology stack as you are using, but hopefully useful to somebody.
与您使用的技术堆栈不同,但希望对某些人有用。
回答by Christophe Eblé
wininet.dllreturns both standard and non-standard status codes that are listed below.
wininet.dll返回下面列出的标准和非标准状态代码。
401 - Unauthorized file
403 - Forbidden file
404 - File Not Found
500 - some inclusion or functions may missed
200 - Completed
12002 - Server timeout
12029,12030, 12031 - dropped connections (either web server or DB server)
12152 - Connection closed by server.
13030 - StatusText properties are unavailable, and a query attempt throws an exception
For the status code "zero" are you trying to do a request on a local webpage running on a webserver or without a webserver?
对于状态代码“零”,您是尝试在运行在网络服务器上的本地网页上还是在没有网络服务器的情况下执行请求?
XMLHttpRequest status = 0 and XMLHttpRequest statusText = unknowncan help you if you are not running your script on a webserver.
XMLHttpRequest status = 0 和 XMLHttpRequest statusText = unknown如果您不是在网络服务器上运行脚本,则可以为您提供帮助。
回答by mike nelson
Workaround: what we ended up doing
解决方法:我们最终做了什么
We figured it was to do with firewall issues, and so we came up with a workaround that did the trick. If anyone has this same issue, here's what we did:
我们认为这与防火墙问题有关,因此我们想出了一种解决方法来解决问题。如果有人有同样的问题,这就是我们所做的:
We still write the data to a text file on the local hard disk as we previously did, using an HTA.
When the user clicks "send data back to server", the HTA reads in the data and writes out an HTML page that includes that data as an XML data island (actually using a SCRIPT LANGUAGE=XML script block).
The HTA launches a link to the HTML page in the browser.
The HTML page now contains the javascript that posts the data to the server (using Microsoft.XMLHTTP).
我们仍然像以前一样使用 HTA 将数据写入本地硬盘上的文本文件。
当用户单击“将数据发送回服务器”时,HTA 读入数据并写出包含该数据的 HTML 页面作为 XML 数据岛(实际上使用 SCRIPT LANGUAGE=XML 脚本块)。
HTA 在浏览器中启动指向 HTML 页面的链接。
HTML 页面现在包含将数据发布到服务器的 javascript(使用 Microsoft.XMLHTTP)。
Hope this helps anyone with a similar requirement. In this case it was a Flash game used on a laptop at tradeshows. We never had access to the laptop and could only email it to the client as this tradeshow was happening in another country.
希望这可以帮助任何有类似要求的人。在这种情况下,它是在贸易展览会上在笔记本电脑上使用的 Flash 游戏。我们从未接触过笔记本电脑,只能通过电子邮件将其发送给客户,因为该贸易展正在另一个国家/地区举行。
回答by Cory Danielson
An HTTP response code of 0 indicates that the AJAX request was cancelled.
HTTP 响应代码 0 表示 AJAX 请求已取消。
This can happen either from a timeout, XHR abortion or a firewall stomping on the request. A timeout is common, it means the request failed to execute within a specified time. An XHR Abortion is very simple to do... you can actually call .abort() on an XMLHttpRequest object to cancel the AJAX call. (This is good practice for a single page application if you don't want AJAX calls returning and attempting to reference objects that have been destroyed.) As mentioned in the marked answer, a firewall would also be capable of cancelling the request and trigger this 0 response.
这可能是由于超时、XHR 中止或防火墙对请求的踩踏而发生的。超时很常见,这意味着请求未能在指定时间内执行。XHR Abortion 的操作非常简单……您实际上可以在 XMLHttpRequest 对象上调用 .abort() 来取消 AJAX 调用。(如果您不希望 AJAX 调用返回并尝试引用已销毁的对象,这对于单页应用程序来说是一种很好的做法。)如标记答案中所述,防火墙也能够取消请求并触发此操作0 响应。
XHR Abort: Abort Ajax requests using jQuery
XHR Abort:使用 jQuery 中止 Ajax 请求
var xhr = $.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
//kill the request
xhr.abort()
It's worth noting that running the .abort() method on an XHR object will also fire the error callback. If you're doing any kind of error handling that parses these objects, you'll quickly notice that an aborted XHR and a timeout XHR are identical, but with jQuery the textStatus that is passed to the error callback will be "abort" when aborted and "timeout" with a timeout occurs. If you're using Zepto (very very similar to jQuery) the errorType will be "error" when aborted and "timeout" when a timeout occurs.
值得注意的是,在 XHR 对象上运行 .abort() 方法也会触发错误回调。如果您正在执行解析这些对象的任何类型的错误处理,您会很快注意到中止的 XHR 和超时的 XHR 是相同的,但是对于 jQuery,传递给错误回调的 textStatus 将在中止时“中止”和“超时”发生超时。如果您使用 Zepto(与 jQuery 非常相似),则中止时 errorType 为“error”,超时时为“timeout”。
jQuery: error(jqXHR, textStatus, errorThrown);
Zepto: error(xhr, errorType, error);
回答by Brad Parks
As detailed by this answer on this page, a status code of 0 means the request failed for some reason, and a javascript library interpreted the fail as a status code of 0.
正如本页上的这个答案所详述的那样,状态代码 0 表示由于某种原因请求失败,并且 javascript 库将失败解释为状态代码 0。
To test this you can do either of the following:
要对此进行测试,您可以执行以下任一操作:
1) Use this chrome extension, Requestlyto redirect your url from the httpsversion of your url to the httpversion, as this will cause a mixed content security error, and ultimately generate a status code of 0. The advantage of this approach is that you don't have to change your app at all, and you can simply "rewrite" your url using this extension.
1)使用这个chrome扩展,Requestly将你的url从你的urlhttps版本重定向到http版本,因为这会导致混合内容安全错误,最终生成状态码0。这种方式的好处是你不根本不必更改您的应用程序,您只需使用此扩展程序“重写”您的网址即可。
2) Change the code of your app to optionally make your endpoint redirect to the httpversion of your url instead of the httpsversion (or vice versa). If you do this, the request will fail with status code 0.
2) 更改您的应用程序的代码,以选择性地使您的端点重定向到http您的 urlhttps版本而不是版本(反之亦然)。如果这样做,请求将失败,状态代码为 0。
回答by fellowworldcitizen
In my case the status became 0 when i would forget to put the WWW in front of my domain. Because all my ajax requests were hardcoded http:/WWW.mydomain.com and the webpage loaded would just be http://mydomain.comit became a security issue because its a different domain. I ended up doing a redirect in my .htaccess file to always put www in front.
在我的情况下,当我忘记将 WWW 放在我的域前面时,状态变为 0。因为我所有的 ajax 请求都是硬编码的 http://WWW.mydomain.com 并且加载的网页只是http://mydomain.com它成为一个安全问题,因为它是一个不同的域。我最终在我的 .htaccess 文件中进行了重定向,以始终将 www 放在前面。
回答by Jonathan Amend
I found a new and undocumented reason for status == 0. Here is what I had:
我找到了 status == 0 的一个新的和未记录的原因。这是我所拥有的:
XMLHttpRequest.status === 0
XMLHttpRequest.readyState === 0
XMLHttpRequest.responseText === ''
XMLHttpRequest.state() === 'rejected'
It was not cross-origin, network, or due to cancelled requests (by code or by user navigation). Nothing in the developer console or network log.
这不是跨域、网络或由于取消的请求(通过代码或用户导航)。开发者控制台或网络日志中没有任何内容。
I could find very little documentation on state() (Mozilla does not list it, W3C does) and none of it mentioned "rejected".
我能找到关于 state() 的文档很少(Mozilla 没有列出它,W3C 有)并且没有一个提到“拒绝”。
Turns out it was my ad blocker(uBlock Origin on Firefox).
原来这是我的广告拦截器(Firefox 上的 uBlock Origin)。
回答by McX
In addition to Lee's answer, you may find more information about the real cause by switching to synchronousrequests, as you'll get also an exception :
除了Lee 的回答之外,您还可以通过切换到同步请求找到有关真正原因的更多信息,因为您还会得到一个异常:
function request(url) {
var request = new XMLHttpRequest();
try {
request.open('GET', url, false);
request.send(null);
} catch (e) {
console.log(url + ': ' + e);
}
}
For example :
例如 :
NetworkError: A network error occurred.
网络错误:发生网络错误。

