为什么 AJAX 返回 HTTP 状态代码 0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2000609/
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
Why is AJAX returning HTTP status code 0?
提问by tarnfeld
回答by mnk
Another case:
另一个案例:
It could be possible to get a status code of 0if you have sent an AJAX call and a refresh of the browser was triggered before getting the AJAX response. The AJAX call will be cancelled and you will get this status.
0如果您发送了 AJAX 调用并且在获得 AJAX 响应之前触发了浏览器的刷新,则可能会获得状态代码。AJAX 调用将被取消,您将获得此状态。
回答by Langdon
In my experience, you'll see a status of 0 when:
根据我的经验,在以下情况下您会看到状态为 0:
- doing cross-site scripting (where access is denied)
- requesting a URL that is unreachable (typo, DNS issues, etc)
- the request is otherwise intercepted (check your ad blocker)
- as above, if the request is interrupted (browser navigates away from the page)
- 做跨站脚本(拒绝访问)
- 请求无法访问的 URL(错别字、DNS 问题等)
- 请求被拦截(检查您的广告拦截器)
- 如上所述,如果请求被中断(浏览器导航离开页面)
回答by Rafi
Same problem here when using <button onclick="">submit</button>. Then solved by using <input type="button" onclick="">
使用<button onclick="">submit</button>. 然后通过使用解决<input type="button" onclick="">
回答by Jagadeesh
Status code 0 means the requested url is not reachable. By changing http://something/somethingto https://something/somethingworked for me. IE throwns an error saying "permission denied" when the status code is 0, other browsers dont.
状态代码 0 表示无法访问请求的 url。通过将http://something/something更改为https://something/something对我有用。当状态码为 0 时,IE 会抛出“权限被拒绝”的错误,其他浏览器不会。
回答by nuxxxx
It is important to note, that ajax calls can fail even within a session which is defined by a cookie with a certain domain prefixed with www. When you then call your php script e.g. without the www. prefix in the url, the call will fail and viceversa, too.
需要注意的是,即使在由具有以 www 为前缀的特定域的 cookie 定义的会话中,ajax 调用也可能失败。当你然后调用你的 php 脚本时,例如没有 www。url 中的前缀,调用将失败,反之亦然。
回答by Martin Vseticka
This articlehelped me. I was submitting form via AJAX and forgotten to use return false(after my ajax request) which led to classic form submission but strangely it was not completed.
这篇文章帮助了我。我正在通过 AJAX 提交表单并忘记使用return false(在我的 ajax 请求之后),这导致了经典的表单提交,但奇怪的是它没有完成。
回答by starwed
I found another case where jquery gives you status code 0 -- if for some reason XMLHttpRequest is not defined, you'll get this error.
我发现了另一种情况,jquery 为您提供状态代码 0——如果由于某种原因未定义 XMLHttpRequest,您将收到此错误。
Obviously this won't normally happen on the web, but a bug in a nightly firefox build caused this to crop up in an add-on I was writing. :)
显然,这通常不会发生在网络上,但是夜间 Firefox 构建中的一个错误导致这在我正在编写的附加组件中突然出现。:)
回答by michael
Because this shows up when you google ajax status 0 I wanted to leave some tip that just took me hours of wasted time... I was using ajax to call a PHP service which happened to be Phil's REST_Controller for Codeigniter (not sure if this has anything to do with it or not) and kept getting status 0, readystate 0 and it was driving me nuts. I was debugging it and noticed when I would echo and return instead of exit the message I'd get a success. Finally I turned debugging off and tried and it worked. Seems the xDebug debugger with PHP was somehow modifying the response. If your using a PHP debugger try turning it off to see if that helps.
因为这会在您 google ajax status 0 时出现,所以我想留下一些提示,这只是浪费了我几个小时的时间……我正在使用 ajax 调用 PHP 服务,该服务恰好是 Phil 的 REST_Controller for Codeigniter(不确定这是否有与它无关)并不断获得状态 0、就绪状态 0,这让我发疯。我正在调试它并注意到我什么时候会回显并返回而不是退出我会成功的消息。最后我关闭了调试并尝试了它。似乎带有 PHP 的 xDebug 调试器以某种方式修改了响应。如果您使用 PHP 调试器,请尝试将其关闭以查看是否有帮助。
回答by Ricardo Rivaldo
I had the same problem, and it was related to XSS (cross site scripting) block by the browser. I managed to make it work using a server.
我遇到了同样的问题,它与浏览器的 XSS(跨站点脚本)块有关。我设法使用服务器使其工作。
Take a look at: http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/282972/why-am-i-getting-xmlhttprequest.status0
看看:http: //www.daniweb.com/web-development/javascript-dhtml-ajax/threads/282972/why-am-i-getting-xmlhttprequest.status0
回答by Vlad Schnakovszki
In my case, it was caused by running my django server under http://127.0.0.1:8000/but sending the ajax call to http://localhost:8000/. Even though you would expect them to map to the same address, they don't so make sure you're not sending your requests to localhost.
就我而言,它是由于在 django 服务器下运行http://127.0.0.1:8000/但将 ajax 调用发送到http://localhost:8000/. 即使您希望它们映射到相同的地址,它们也不会因此确保您没有将请求发送到 localhost。

