javascript XMLHttpRequest 未定义,在 chrome 扩展选项页面中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15874535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:24:45  来源:igfitidea点击:

XMLHttpRequest is not defined, in a chrome extension options page

javascriptgoogle-chrome-extensionxmlhttprequest

提问by little-dude

I'm trying to make an XMLHttpRequest in the options page of an extension. In my options.jsfile I simply have the following code :

我正在尝试在扩展的选项页面中创建一个 XMLHttpRequest。在我的options.js文件中,我只有以下代码:

if (window.XMLHttpRequest){
        var xhr = new getXMLHttpRequest();
}

But I have this error in the console

但是我在控制台中有这个错误

Uncaught ReferenceError: getXMLHttpRequest is not defined

未捕获的 ReferenceError:未定义 getXMLHttpRequest

I saw herethat getXMLHttpRequests are a problem for hosted apps, but in this case, it's a simple extension, so I don't understand.

我在这里看到 getXMLHttpRequests 是托管应用程序的问题,但在这种情况下,它是一个简单的扩展,所以我不明白。

回答by Quentin

To construct an XHR object you use new XMLHttpRequest();.

要构造 XHR 对象,请使用new XMLHttpRequest();.

getXMLHttpRequestis not a standard function.

getXMLHttpRequest不是标准功能。

I saw here that getXMLHttpRequests are a problem…

我在这里看到 getXMLHttpRequests 是一个问题......

The question at the other end of the link doesn't use a function with a name starting with get.

链接另一端的问题不使用名称以get开头的函数。

回答by PAT

You can use

您可以使用

function GetXmlHttpObject()
{ 
    var objXMLHttp=null;
    if (window.XMLHttpRequest)
    {
        objXMLHttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return objXMLHttp;
}