FF extension - getting xmlhttp.status==0

那年仲夏 提交于 2019-12-23 17:50:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!