3

我正在为 Firefox 编写扩展,它使用page-mod模块来运行包含以下内容的 JavaScript 文件:

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);

我不断得到xmlhttp.status==0而不是200,即使localhost我使用 IP 地址而不是。有任何想法吗?

4

1 回答 1

2

内容脚本代码无法进行跨域请求 - 请尝试使用 Request 模块:

https://addons.mozilla.org/en-US/developers/docs/sdk/1.1/packages/addon-kit/docs/request.html

您可以在附加组件的 main.js 脚本中实现请求,而不是在单独的脚本中编写代码并使用 page-mod 将其注入页面。

于 2011-09-15T17:40:57.540 回答