无法使用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();
它会提醒“状态0”。
本地主机请求(Cd)的情况相同_目录文件保存为本地文件)
xmlhttp.open("GET","http://localhost/cd_catalog.xml", true);
但是使用本地主机ip请求
xmlhttp.open("GET","http://127.0.0.1/cd_catalog.xml", true);
和本地文件请求
xmlhttp.open("GET","cd_catalog.xml", true);
一切正常(状态200)
什么会导致在线请求的问题(Status=0)?LiveHTTP报头显示,在所有4种情况下,一切都正常:
HTTP/1.1 200 OK
Content-Length: 4742
发布于 2018-03-12 15:27:54
当包含脚本的html文件通过文件协议在浏览器中打开时,状态为0。确保将文件放在服务器(Apache或tomcat之类的)中,然后通过浏览器中的http协议打开它。http://localhost/myfile.html)这是解决办法。
发布于 2018-03-12 16:05:48
如果您正在进行本地主机开发,您可以进行跨域调用--我一直都在这样做。对于Firefox,您必须在配置设置中启用它。
signed.applets.codebase_principal_support = true
然后将类似的内容添加到您的XHR打开代码中:
if (isLocalHost()){
if (typeof(netscape) != 'undefined' && typeof(netscape.security) != 'undefined'){
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
}
}
对于IE,如果我没记错的话,您所要做的就是在“杂项”下启用浏览器的安全设置。
https://stackoverflow.com/questions/-100007590
复制相似问题