XMLHttpRequest() does not work in the latest versions of Firefox?

徘徊边缘 提交于 2019-12-25 18:26:45

问题


In my addons I always used new XMLHttpRequest () and it worked perfectly. Now all requests ajax stopped working.

Currently new XMLHttpRequest () is causing the following error: ReferenceError: XMLHttpRequest is not defined

So I changed my code to:

try {
    var XMLHttpRequest;
    if (typeof(XMLHttpRequest) == "undefined")
        XMLHttpRequest = content.XMLHttpRequest;
}
catch (e) {
    alert(e);
}
var xmlhttp = new XMLHttpRequest();
...

Sometimes the request usually works, but sometimes not.

The code "alert(e);" is never executed, then there is no error there.

I can not understand why sometimes it works and sometimes not. Previously I used only var xmlhttp = new XMLHttpRequest(); and always worked.

Now how do I create a new ajax request?


回答1:


As I said in a comment, when you are running in the context of a browser window (like code loaded by an overlay to that window) then XMLHttpRequest should definitely be available. I verified that just in case and it works for me.

But in case everything else fails you can still instantiate the XPCOM component corresponding to XMLHttpRequest directly:

var xmlhttp = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
                        .createInstance(Components.interfaces.nsIXMLHttpRequest);
xmlhttp.open(...);


来源:https://stackoverflow.com/questions/12273313/xmlhttprequest-does-not-work-in-the-latest-versions-of-firefox

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