FF 扩展 - 获取 xmlhttp.status==0

发布于 2024-11-30 19:45:35 字数 792 浏览 3 评论 0 原文

我正在为 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,即使我使用的是 IP 地址而不是 localhost。 有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒的傷心 2024-12-07 19:45:36

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文