问题描述
无法通过 XMLHttpRequest 获取数据(状态 0 且 responseText 为空):
<上一页>xmlhttp=新的 XMLHttpRequest();xmlhttp.open("GET","http://www.w3schools.com/XML/cd_catalog.xml", true);xmlhttp.onreadystatechange=函数(){如果(xmlhttp.readyState==4)警报(状态" + xmlhttp.status);}xmlhttp.send();它会提醒状态 0".
与localhost请求相同的情况(cd_catalog.xml保存为本地文件)
<上一页>xmlhttp.open("GET","http://localhost/cd_catalog.xml", true);但是使用 localhost IP 请求
<上一页>xmlhttp.open("GET","http://127.0.0.1/cd_catalog.xml", true);并与本地文件请求
<上一页>xmlhttp.open("GET","cd_catalog.xml", true);一切正常(状态 200)
在线请求出现问题 (status=0) 的原因是什么?
PS:Live HTTP Headers 显示在所有 4 种情况下一切正常:
<上一页>HTTP/1.1 200 正常内容长度:4742PS2:VMWare 上的 Apache 本地 Web 服务器(主机操作系统 Win7、来宾操作系统 Ubuntu、网络适配器 – NAT).浏览器 – 火狐.
当你的包含脚本的 html 文件通过文件方案在浏览器中打开时,状态为 0.确保将文件放在您的服务器(apache 或 tomcat 等)中,然后在浏览器中通过 http 协议打开它.(即 http://localhost/myfile.html)这就是解决方案.
Cannot get data with XMLHttpRequest (status 0 and responseText is empty):
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".
The same situation with the localhost request (cd_catalog.xml is saved as a local file)
xmlhttp.open("GET","http://localhost/cd_catalog.xml", true);
But with the localhost IP request
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)
What can cause the problem (status=0) with the online request?
PS: Live HTTP Headers shows that everything is OK in all 4 cases:
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.
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.
这篇关于XMLHttpRequest 状态 0(responseText 为空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!