7

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

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

But I have this error in the console

Uncaught ReferenceError: getXMLHttpRequest is not defined

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

Community
  • 1
  • 1
little-dude
  • 1,544
  • 2
  • 17
  • 33
  • 7
    I got the error "XMLHttpRequest is not defined" in `background.js`, for a different reason -- because Chrome extensions with Manifest v3 [use a service worker](https://developer.chrome.com/docs/extensions/mv3/intro/mv3-overview/#service-workers) instead of a background page or background script, and [XMLHttpRequest is not available in service workers](https://stackoverflow.com/a/38393563/3345375). Thus it's necessary to use the Fetch API instead. – jkdev Nov 18 '21 at 05:07

2 Answers2

5

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

getXMLHttpRequest is not a standard function.

I saw here that getXMLHttpRequests are a problem…

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-2

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;
}
PAT
  • 1