问题
I'm writing an extension for Firefox and it is using page-mod
module to run a JavaScript file containing:
function handleServerResponse() {
if (xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
//some code
}
else {
alert("Error during AJAX call. Please try again");
}
}
}
var xmlHttp = new XMLHttpRequest();
var txtname = document.getElementById("txtname");
xmlHttp.open("POST","http://localhost:8080/Jelly/GetStuff",true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.send("url=" + document.URL);
i'm keep getting xmlhttp.status==0
and not 200
, even if instead of localhost
I use the IP address.
Any ideas?
回答1:
Content script code can't do cross-domain requests - try using the Request module instead:
https://addons.mozilla.org/en-US/developers/docs/sdk/1.1/packages/addon-kit/docs/request.html
Instead of writing the code in a separate script and injecting it into a page using a page-mod, you can implement the request in the main.js script in your add-on.
来源:https://stackoverflow.com/questions/7131096/ff-extension-getting-xmlhttp-status-0