Javascript XMLHttpRequest 状态 0(响应文本为空)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5005960/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:13:31  来源:igfitidea点击:

XMLHttpRequest status 0 (responseText is empty)

javascriptajaxxmlhttprequest

提问by arigasa

Cannot get data with XMLHttpRequest (status 0 and responseText is empty):

无法使用 XMLHttpRequest 获取数据(状态 0 且 responseText 为空):

xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://www.w3schools.com/XML/cd_catalog.xml", true);
xmlhttp.onreadystatechange=function() 
{
  if(xmlhttp.readyState==4)
    alert("status " + xmlhttp.status);
}
xmlhttp.send();

It alerts "status 0".

它提醒“状态 0”。

The same situation with the localhost request (cd_catalog.xml is saved as a local file)

与localhost请求相同的情况(cd_catalog.xml保存为本地文件)

xmlhttp.open("GET","http://localhost/cd_catalog.xml", true);

But with the localhost IP request

但是使用本地主机 IP 请求

xmlhttp.open("GET","http://127.0.0.1/cd_catalog.xml", true);

and with the local file request

并使用本地文件请求

xmlhttp.open("GET","cd_catalog.xml", true);

everything is OK (status 200)

一切正常(状态 200)

What can cause the problem (status=0) with the online request?

什么会导致在线请求出现问题(状态=0)?

PS: Live HTTP Headers shows that everything is OK in all 4 cases:

PS:Live HTTP Headers 显示在所有 4 种情况下一切正常:

  HTTP/1.1 200 OK
  Content-Length: 4742

PS2: Apache local web server on VMWare (host OS Win7, Guest OS Ubuntu, Network adapter – NAT). Browser – Firefox.

PS2:VMWare 上的 Apache 本地 Web 服务器(主机操作系统 Win7、来宾操作系统 Ubuntu、网络适配器 – NAT)。浏览器 - 火狐。

回答by Abhishek_8

status is 0 when your html file containing the script is opened in the browser via the file scheme. Make sure to place the files in your server (apache or tomcat whatever) and then open it via http protocol in the browser. (i.e. http://localhost/myfile.html) This is the solution.

当包含脚本的 html 文件通过文件方案在浏览器中打开时,状态为 0。确保将文件放在您的服务器(apache 或 tomcat 等)中,然后在浏览器中通过 http 协议打开它。(即http://localhost/myfile.html)这是解决方案。

回答by Slammer

The cause of your problems is that you are trying to do a cross-domain call and it fails.

您的问题的原因是您正在尝试进行跨域调用并且失败了

If you're doing localhost development you can make cross-domain calls - I do it all the time.

如果您正在进行本地主机开发,您可以进行跨域调用 - 我一直都在这样做。

For Firefox, you have to enable it in your config settings

对于 Firefox,您必须在配置设置中启用它

signed.applets.codebase_principal_support = true

Then add something like this to your XHR open code:

然后将这样的内容添加到您的 XHR 打开代码中:

  if (isLocalHost()){
    if (typeof(netscape) != 'undefined' && typeof(netscape.security) != 'undefined'){
      netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
    }
  }

For IE, if I remember right, all you have to do is enable the browser's Security setting under "Miscellaneous → Access data sources across domains" to get it to work with ActiveX XHRs.

对于 IE,如果我没记错的话,您所要做的就是在“杂项 → 跨域访问数据源”下启用浏览器的安全设置,以使其与 ActiveX XHR 一起使用。

IE8 and above also added cross-domain capabilities to the native XmlHttpRequest objects, but I haven't played with those yet.

IE8 及更高版本还为原生 XmlHttpRequest 对象添加了跨域功能,但我还没有玩过这些功能。

回答by dyuan

Actually make sure your button type is Button not Submit, that caused status conflict where I met recently.

实际上确保您的按钮类型是按钮不是提交,这导致了我最近遇到的状态冲突。

回答by Alex Robinson

If the server responds to an OPTIONS method and to GET and POST (whichever of them you're using) with a header like:

如果服务器响应 OPTIONS 方法以及 GET 和 POST(无论您使用的是哪个),并带有如下标题:

Access-Control-Allow-Origin: *

It might work OK. Seems to in FireFox 3.5 and rekonq 0.4.0. Apparently, with that header and the initial response to OPTIONS, the server is saying to the browser, "Go ahead and let this cross-domain request go through."

它可能工作正常。似乎在 FireFox 3.5 和 rekonq 0.4.0 中。显然,使用该标头和对 OPTIONS 的初始响应,服务器对浏览器说:“继续,让这个跨域请求通过。”

回答by Andrea Savojardo

Consider also the request timeout:

还要考虑请求超时

Modern browser return readyState=4and status=0if too much time passes before the server response.

如果在服务器响应之前经过了太多时间,现代浏览器将返回readyState=4和 s tatus=0

回答by Ivan

Add setRequestHeader("Access-Control-Allow-Origin","*")to your server response.

添加setRequestHeader("Access-Control-Allow-Origin","*")到您的服务器响应。

回答by Reyan

I had faced a similar problem. Every thing was okay, the "readystate" was 4, but the "status" was 0. It was because I was using a Apache PHP portable server and my file in which I used the "XMLHttpRequest" object was a html file. I changed the file extension to php and the problem was solved.

我遇到过类似的问题。一切正常,“就绪状态”为 4,但“状态”为 0。这是因为我使用的是 Apache PHP 便携式服务器,而我使用“XMLHttpRequest”对象的文件是一个 html 文件。我把文件扩展名改成php,问题就解决了。

回答by Jarekczek

Open javascript console. You'll see an error message there. In my case it was CORS.

打开javascript 控制台。您会在那里看到一条错误消息。就我而言,它是 CORS。

回答by Maya

To see what the problem is, when you get the cryptic error 0 go to ... | More Tools | Developer Tools (Ctrl+Shift+I) in Chrome (on the page giving the error)

要查看问题是什么,当您收到神秘错误 0 时,请转到 ... | 更多工具 | Chrome 中的开发人员工具 (Ctrl+Shift+I)(在给出错误的页面上)

Read the red text in the log to get the true error message. If there is too much in there, right-click and Clear Console, then do your last request again.

阅读日志中的红色文本以获取真正的错误消息。如果那里有太多,右键单击并清除控制台,然后再次执行上次请求。

My first problem was, I was passing in Authorization headers to my own cross-domain web service for the browser for the first time.

我的第一个问题是,我第一次将 Authorization 标头传递给我自己的浏览器跨域 Web 服务。

I already had:

我已经有了:

Access-Control-Allow-Origin: *

But not:

但不是:

Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Authorization

in the response header of my web service.

在我的 Web 服务的响应标头中。

After I added that, my error zero was gone from my own web server, as well as when running the index.html file locally without a web server, but was still giving errors in code pen.

添加后,我自己的 Web 服务器以及在没有 Web 服务器的情况下在本地运行 index.html 文件时,我的错误零消失了,但仍然在代码笔中出现错误。

Back to ... | More Tools | Developer Tools while getting the error in codepen, and there is clearly explained: codepen uses https, so I cannot make calls to http, as the security is lower.

回到... | 更多工具 | 开发者工具同时在codepen中得到错误,并且有明确的解释:codepen使用https,所以我无法调用http,因为安全性较低。

I need to therefore host my web service on https.

因此,我需要在 https 上托管我的 Web 服务。

Knowing how to get the true error message - priceless!

知道如何获得真正的错误信息 - 无价!

回答by Tomi Aarnio

To answer the question of why http://127.0.0.1/cd_catalog.xmlworks while http://localhost/cd_catalog.xmldoesn't: Firefox is treating 127.0.0.1 and localhost as two different domains.

要回答为什么http://127.0.0.1/cd_catalog.xml有效而 http://localhost/cd_catalog.xml无效的问题:Firefox 将 127.0.0.1 和 localhost 视为两个不同的域。