-2

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?

  • Yes, of *course* XHR works. Q: What does your (failing) code look like *before* you put in "var XMLHttpRequest"? Q: are you doing anything different from [this](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest?redirectlocale=en-US&redirectslug=XMLHttpRequest)? Q: What version of Firefox is failing on you? What version last worked for you? – paulsm4 Sep 05 '12 at 01:24
  • 5
    PS: I would *not* name your variable "XMLHttpRequest" ;) Try "xhr", or something like that ;) – paulsm4 Sep 05 '12 at 01:26
  • @paulsm4 - You may want to put that as an answer. – James Black Sep 05 '12 at 01:28
  • @paulsm4 I was just using xmlhttp = new XMLHttpRequest(); before and it worked perfectly. But now with the Firefox 15.0 does not work. – Claudio Henrique da Silva Sep 05 '12 at 04:07
  • @paulsm4 When I was using simply var xmlhttp = new XMLHttpRequest();, an error occurred: "ReferenceError: XMLHttpRequest is not defined". So I tried to fix the problem of XMLHttpRequest does not exist creating a variable with the same name. – Claudio Henrique da Silva Sep 05 '12 at 04:09
  • @ClaudioHenriquedaSilva: You say that this isn't the Add-on SDK. What context does this code run in? Script loaded from an overlay? JavaScript module? XPCOM component? Btw, `var XMLHttpRequest` will make sure that `XMLHttpRequest` is undefined - you should indeed name that variable differently. – Wladimir Palant Sep 05 '12 at 06:47
  • @WladimirPalant The script is being loaded from a overlay (through the .xul file). Again: "var XMLHttpRequest" was just an attempt to fix the problem that XMLHttpRequest is undefined. – Claudio Henrique da Silva Sep 05 '12 at 15:33
  • @ClaudioHenriquedaSilva: Well, works for me in Firefox 15 and Firefox 18.0a1 - `XMLHttpRequest` is definitely defined in the context of the browser window. Unless some extension did `var XMLHttpRequest` or something similar. – Wladimir Palant Sep 05 '12 at 16:56

1 Answers1

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(...);
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Thank you. I'll try to find what was causing XMLHttpRequest stay unavailable. But if I do not find it, I will follow your advice and instantiate the XPCOM component corresponding to XMLHttpRequest directly. – Claudio Henrique da Silva Sep 17 '12 at 13:26