5

I have tried with this code in JavaScript as follows:

var XMLHttpRequest = require("xmlhttprequest");

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.codecademy.com/",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 comes out saying that 'TypeError: XMLHttpRequest is not a constructor'. I appreciate for any help for why this error. Thanks.

2
  • It's got to be because the first line is failing. Commented Mar 9, 2018 at 19:52
  • node.js hasn't direct Ajax- API's. You must use Third-Party modules or the net component from node.js Commented Mar 9, 2018 at 19:56

2 Answers 2

11

The first line should be:

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

See the documentation.

Sign up to request clarification or add additional context in comments.

Comments

2

I think you need to load the module as smlhttprequest and demo

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();

1 Comment

Thanks for your answer, definitely helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.