XMLHtt prequest状态0(responseText的是空) [英] XMLHttpRequest status 0 (responseText is empty)
问题描述
不能与XMLHtt prequest数据(状态0和responseText的是空的):
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();
据警报状态0。
It alerts "status 0".
同样的情况与本地主机请求(cd_catalog.xml保存为本地文件)
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);
但与本地主机的IP请求
But with the localhost IP request
xmlhttp.open("GET","http://127.0.0.1/cd_catalog.xml", true);
和使用本地文件的请求
xmlhttp.open("GET","cd_catalog.xml", true);
一切正常(状态200)
everything is OK (status 200)
有什么可以与在线请求导致问题(状态= 0)?
What can cause the problem (status=0) with the online request?
PS:现场HTTP头显示,一切正常在所有4个情况:
PS: Live HTTP Headers shows that everything is OK in all 4 cases:
HTTP/1.1 200 OK
Content-Length: 4742
PS2:在VMWare(主机操作系统Win7的,客户机操作系统Ubuntu的,网络适配器 - NAT)的Apache本地Web服务器。浏览器 - 火狐
PS2: Apache local web server on VMWare (host OS Win7, Guest OS Ubuntu, Network adapter – NAT). Browser – Firefox.
推荐答案
你的问题的原因是您正在尝试做一个跨域调用和失败
如果你正在做本地主机的发展可以使跨域调用 - 我做这一切的时候。
If you're doing localhost development you can make cross-domain calls - I do it all the time.
对于Firefox,您必须启用它在你的配置设置
For Firefox, you have to enable it in your config settings
signed.applets.codebase_principal_support = true
然后添加像这样的东西你XHR开放code:
Then add something like this to your XHR open code:
if (isLocalHost()){
if (typeof(netscape) != 'undefined' && typeof(netscape.security) != 'undefined'){
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
}
}
有关IE浏览器,如果我记得没错,你所要做的就是让下浏览器的安全设置杂项与RARR;跨域访问数据源得到它使用ActiveX XHRs工作。
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.
IE8及以上还增加了跨域功能,本机XmlHtt prequest对象,但我还没有玩过这些呢。
IE8 and above also added cross-domain capabilities to the native XmlHttpRequest objects, but I haven't played with those yet.
这篇关于XMLHtt prequest状态0(responseText的是空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!