I have tried with this code in JavaScript as follows:
var XMLHttpRequest = require("xmlhttprequest");
var xhr = new XMLHttpRequest();
xhr.open("GET", "/",true);
xhr.send();
console.log(xhr.status);
console.log(xhr.statusText);
When I run this code using node.js at Command Prompt "node ***.js", an error message es out saying that 'TypeError: XMLHttpRequest is not a constructor'. I appreciate for any help for why this error. Thanks.
I have tried with this code in JavaScript as follows:
var XMLHttpRequest = require("xmlhttprequest");
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.codecademy./",true);
xhr.send();
console.log(xhr.status);
console.log(xhr.statusText);
When I run this code using node.js at Command Prompt "node ***.js", an error message es out saying that 'TypeError: XMLHttpRequest is not a constructor'. I appreciate for any help for why this error. Thanks.
Share Improve this question asked Mar 9, 2018 at 19:51 TonyTony 511 gold badge1 silver badge2 bronze badges 2- It's got to be because the first line is failing. – Scott Marcus Commented Mar 9, 2018 at 19:52
-
node.js
hasn't direct Ajax- API's. You must use Third-Party modules or thenet
ponent fromnode.js
– Adrian Preuss Commented Mar 9, 2018 at 19:56
2 Answers
Reset to default 11The first line should be:
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
See the documentation.
I think you need to load the module as smlhttprequest and demo
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();