XMLHttpRequest不接收任何数据或只是“未定义” [英] XMLHttpRequest receiving no data or just "undefined"
问题描述
我尝试制作一个Firefox插件,它使用Javascript运行XMLHttp请求。我想从这个请求中获取数据并发送给* .body.innerhtml。
这是我的代码到目前为止...
var xhr = new XMLHttpRequest();
xhr.open(GET,http://xx.xxxxx.com,true);
xhr.send();
setTimeout(function(){set_body(xhr.responseHtml);},6000);
而不是接收数据,我得到undefined。如果我将xhr.responseHtml更改为responseText,我什么也得不到。我不知道为什么我什么也得不到我正在使用Firefox 12.0开发Ubuntu 12.04 LTS。
如果您需要脚本的更多细节,请询问!
更新:
set_body函数
document.body .innerHTML ='';
document.body.innerHTML = body;
document.close();
更新已解决:
我必须确定RequestHeaders(在xhr.open之后):
$ b $ pre $ xhr.setRequestHeader(主机,xxx);
以下项目:主机,来源和引用者。所以看起来有相同的来源政策真的有问题。
但现在它工作!感谢所有!
i try to make a Firefox Addon which runs a XMLHttp Request in Javascript. I want to get the data from this request and send it to *.body.innerhtml.
That's my code so far...
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://xx.xxxxx.com", true);
xhr.send();
setTimeout(function() { set_body(xhr.responseHtml); }, 6000);
Instead of receiving the data, I get "undefined". If I change xhr.responseHtml to responseText I get nothing. I don't know why I'm getting nothing. I'm working on Ubuntu 12.04 LTS with Firefox 12.0.
If you need any more details on the script please ask!
Update:
set_body Function
document.body.innerHTML = '';
document.body.innerHTML = body;
document.close();
Update SOLVED:
I had to determine the RequestHeaders (right after xhr.open):
xhr.setRequestHeader("Host", "xxx");
For following Items: Host, Origin and Referer. So it seems there was really a problem with the same origin policy.
But now it works! Thanks to all!
这篇关于XMLHttpRequest不接收任何数据或只是“未定义”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!