SteMa
SteMa

Reputation: 3022

XMLHttpRequest in Firefox Extension

I am writing a FireFox-Extension and want to load Data from Server. But when I try to initialize the XMLHttpRequest with:

var request = new XMLHttpRequest();

The error console says:

ReferenceError: XMLHttpRequest is not defined

Do I have to include something or why the XMLTttpRequest is not recognized?

Upvotes: 9

Views: 8206

Answers (3)

Wladimir Palant
Wladimir Palant

Reputation: 57651

The Add-on SDK (that you are using indirectly via the Add-on Builder) provides a request package that is essentially a wrapper around XMLHttpRequest, you should use it. From what I understand, you aren't given direct XMLHttpRequest access to prevent issues if the add-on is uninstalled/disabled while a request is being performed.

Upvotes: 11

Yurii Kovalenko
Yurii Kovalenko

Reputation: 419

For using XMLHttpRequest constructor you should add the XPCOM component constructor:

const XMLHttpRequest  = Components.Constructor("@mozilla.org/xmlextras/xmlhttprequest;1", "nsIXMLHttpRequest");

and after:

// some code
var req = new XMLHttpRequest();
// some code

More info on MDN

Upvotes: 4

user1635430
user1635430

Reputation: 66

It seems that Wladimir's answer is useful for most of such cases. But there is one more case.

I've found tris page trying to solve the problem with this error under Firefox 16. Strange, but I've never saw this error under Firefox 15.

I've got this error in content script on some pages. The script was injected at the start:

contentScriptWhen : "start",

It looks like in some cases window object was not initialized correctly. So, I've just changed script loading from start to end. It was possible in my case. The problem has gone...

I don't know why it appears, but know solution and hope that it will be useful for someone.

Upvotes: 1

Related Questions