问题
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