简体   繁体   English

Firefox扩展中的XMLHttpRequest

[英]XMLHttpRequest in Firefox Extension

I am writing a FireFox-Extension and want to load Data from Server. 我正在编写FireFox-Extension并希望从服务器加载数据。 But when I try to initialize the XMLHttpRequest with: 但是,当我尝试使用以下命令初始化XMLHttpRequest时:

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? 我是否必须包含某些内容或为什么无法识别XMLTttpRequest?

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. Add-on SDK(您通过Add-on Builder间接使用)提供了一个request ,它本质上是XMLHttpRequest的包装器,您应该使用它。 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. 根据我的理解,如果在执行请求时卸载/禁用了加载项,则不会给予直接的XMLHttpRequest访问权限以防止出现问题。

For using XMLHttpRequest constructor you should add the XPCOM component constructor: 要使用XMLHttpRequest构造函数,您应该添加XPCOM组件构造函数:

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

and after: 之后:

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

More info on MDN 有关MDN的更多信息

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. 我发现tris页面试图在Firefox 16下解决此错误的问题。奇怪,但我从未在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. 我不知道它为什么出现,但知道解决方案并希望它对某人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM