javascript - XMLHttpRequest receiving no data or just "undefined" - Stack Overflow

admin2025-04-19  2

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", "", 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();

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.", 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();
Share Improve this question asked May 29, 2012 at 21:29 daemondaemon 431 silver badge7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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!

when you set the last param of open to true you are asking for an async event. So you need to add a callback to xhr like so:

xhr.onReadyStateChange = function(){
   // define what you want to happen when server returns
}

that is invoked when the server responds. To test this without async set the third param to false. Then send() will block and wait there until the response es back. Setting an arbitrary timeout of 6 seconds is not the right way to handle this.

This code should work:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  if (xhr.readyState == 4) {
    set_body(xhr.responseText);
  }
};
xhr.open("GET", "http://xx.xxxxx.", true);
xhr.send();

Make sure that you are getting a correct response from URL http://xx.xxxxx.. You may have a problem with cross-domain calls. If you have a page at domain http://first. and you try to do XMLHttpRequest from domain http://second., Firefox will fail silently (there will be no error message, no response, nothing). This is a security measure to prevent XSS (Cross-site scripting).

Anyway, if you do XMLHttpRequest from a chrome:// protocol, it is considered secure and it will work. So make sure you use this code and make the requests from your addon, not from your localhost or something like that.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745041877a281592.html

最新回复(0)