admin管理员组

文章数量:1384752

I am building a Mozilla AddOn, using the AddOn SDK. I need to make an AJAX post, so I'm using XMLHttpRequest. In Mozilla's documentation it says that XMLHttpRequest

can't be instantiated using the XMLHttpRequest() constructor. The constructor is not defined inside ponents and the code results in an error. The best way to work around this is to use the XPCOM ponent constructor.

The docs say to do it this way:

const XMLHttpRequest = Components.Constructor["@mozilla/xmlextras/xmlhttprequest;1"];
var oReq = new XMLHttpRequest();

Fine. So I implement the code exactly as it's shown in the docs:

var { Cc, Cu, Ci, Cr, Cm, ponents } = require('chrome');
const XMLHttpRequest = ponents.Constructor["@mozilla/xmlextras/xmlhttprequest;1"];
. . .
var oReq = new XMLHttpRequest();

And I get a "XMLHttpRequest is not a constructor" error on this line.

What am I missing? What am I doing wrong here? I can't use the "Components.Constructor["@..." way because the AddOn SDK CLI tool that generates the .xpi file plains, saying that

use 'Components' to access chrome authority. To do so, you need to add a line somewhat like the following:

const {ponents} = require("chrome");

Then you can use any shortcuts to its properties that you import from the 'chrome' module ('Cc', 'Ci', 'Cm', 'Cr', and 'Cu' for the 'classes', 'interfaces', 'manager', 'results', and 'utils' properties, respectively. And ponents for Components object itself).

OK, fine. I do have the require(chrome) call at the top of my file, and generate the shortcuts, and then amusing ponents, which is the correct form, and which the message from the AddOn piler says to use. And yet I get the error.

Any help greatly appreciated.

I am building a Mozilla AddOn, using the AddOn SDK. I need to make an AJAX post, so I'm using XMLHttpRequest. In Mozilla's documentation it says that XMLHttpRequest

can't be instantiated using the XMLHttpRequest() constructor. The constructor is not defined inside ponents and the code results in an error. The best way to work around this is to use the XPCOM ponent constructor.

The docs say to do it this way:

const XMLHttpRequest = Components.Constructor["@mozilla/xmlextras/xmlhttprequest;1"];
var oReq = new XMLHttpRequest();

Fine. So I implement the code exactly as it's shown in the docs:

var { Cc, Cu, Ci, Cr, Cm, ponents } = require('chrome');
const XMLHttpRequest = ponents.Constructor["@mozilla/xmlextras/xmlhttprequest;1"];
. . .
var oReq = new XMLHttpRequest();

And I get a "XMLHttpRequest is not a constructor" error on this line.

What am I missing? What am I doing wrong here? I can't use the "Components.Constructor["@..." way because the AddOn SDK CLI tool that generates the .xpi file plains, saying that

use 'Components' to access chrome authority. To do so, you need to add a line somewhat like the following:

const {ponents} = require("chrome");

Then you can use any shortcuts to its properties that you import from the 'chrome' module ('Cc', 'Ci', 'Cm', 'Cr', and 'Cu' for the 'classes', 'interfaces', 'manager', 'results', and 'utils' properties, respectively. And ponents for Components object itself).

OK, fine. I do have the require(chrome) call at the top of my file, and generate the shortcuts, and then amusing ponents, which is the correct form, and which the message from the AddOn piler says to use. And yet I get the error.

Any help greatly appreciated.

Share Improve this question edited Sep 18, 2013 at 19:47 nmaier 33.2k5 gold badges65 silver badges79 bronze badges asked Sep 18, 2013 at 18:31 LewLew 1,4611 gold badge16 silver badges22 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You should be using the request module in the Add-on SDK.

Should you for some reason still want to use nsIXMLHttpRequest directly, please note that the correct way to produce the constructor in an SDK module would be:

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

Except that ponents.Constructor() doesn't seem to work in the SDK. So better create an instance directly:

var {Cc, Ci} = require('chrome');
var r = Cc["@mozilla/xmlextras/xmlhttprequest;1"].
        createInstance(Ci.nsIXMLHttpRequest);
r.open("GET", "http://example/");
r.addEventListener("loadend", function(e) {
    console.log(this, e, e.type, this.responseText);
});
r.send();

The MDN wiki(!) information is simply incorrect. I edited the article now and set the "Technical Review needed" flag.

Or:

const { XMLHttpRequest } = require('sdk/net/xhr');

let r = new XMLHttpRequest();

console.dir(r);
r.open("GET", "http://example/");
r.addEventListener("loadend", function(e) {
    console.log(this, e, e.type, this.responseText);
});
r.send();

See these docs for more info and limitations.

I imported it like this:

var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

But I used Node.js...

本文标签: