responseXML 始终为空

2022-01-15 00:00:00 xmlhttprequest javascript ajax

我用的是firefox 3.6.10,用firebug来调试

Im using firefox 3.6.10, and firebug to debug

所以,这是我的代码:

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url,false);
xmlhttp.setRequestHeader('Content-Type',  'text/xml');
xmlhttp.send(null);
alert(xmlhttp.responseXML);

responseXML 始终为空,我已经在来自不同域的多个 URL 上尝试过它.我也尝试过异步,结果是一样的.responseText 总是正确返回,没有问题.

responseXML is always null, and i've tried it on several URLs from different domains. I have also tried it asynchronously, it's the same result. The responseText is always properly returned, no problems with it.

我的目标是获得 responseXML.documentElement.

感谢您的帮助.

编辑-----------
此 javascript 代码是从 Greasemonkey 用户脚本执行的,我将其设置为与请求的 url 相同的来源.我也尝试从萤火控制台执行,再次确保原产地政策.两者都有相同的错误.
讨厌javascript.

EDIT-----------
This javascript code was executed from a Greasemonkey userscript, i made surte its the same origin as the requested url. Also i tried executing from firebug console, again ensuring the origin policy. Same error on both.
Gotta hate javascript.

推荐答案

我敢打赌你违反了同源政策.

I bet you are violating the same origin policy.

对于 XHR,您必须具有相同的协议、域、端口等.因此,如果您在 localhost:8080/app 上运行应用程序,则不能 ajax 到 www.cnn.com.

For XHRs, you must have the same protocol, domain, port, etc. So if you are running an app on localhost:8080/app, you CANNOT ajax to www.cnn.com.

不同的浏览器有不同的处理方式;我看到FF按照你的描述做了,就是请求看起来正常返回但是没有数据...

Different browsers handle this differently; I have seen FF do what you describe, which is the request appears to return normally but there is no data...

相关文章