问题描述
我为Firefox编写了一个扩展,它使用 page-mod
模块运行一个包含以下内容的JavaScript文件:
$ b $ (){
if(xmlHttp .status == 200){
//一些代码
}
其他{
alert(AJAX调用时出错,请再试一次);
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);
我不断收到 xmlhttp.status == 0
而不是 200
,即使代替 localhost
我使用IP地址。
有任何想法吗?解决方案内容脚本代码不能执行跨域请求 - 尝试使用请求模块:
p>
不是将代码写入单独的脚本中,而是使用page-mod将其注入到页面中,而是可以在附加组件中的main.js脚本中实现该请求。
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?
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.
这篇关于FF扩展 - 获取xmlhttp.status == 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!