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
forComponents
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
forComponents
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 badges3 Answers
Reset to default 3You 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...
本文标签:
版权声明:本文标题:javascript - "XMLHttpRequest is not a constructor" error, contrary do Mozilla docs on usage inside browser chr 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744537131a2611383.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论