-4

My code sample

I checked so many websites and followed their examples, and they all just do the same:

let chr = new XMLHttpRequest();

For some reason I get the above error!

6
  • 2
    Please paste the code / error messages directly instead of supplying an image. Perhaps this post will help. Commented Oct 24, 2018 at 8:14
  • I guess the examples you read are targeted for browsers and you execute the code in nodejs. Commented Oct 24, 2018 at 8:16
  • It's XML, not CML let chr = new XMLHttpRequest() Please check that you wrote the write method name there. Commented Oct 24, 2018 at 8:21
  • please take the time to format your post correctly. How are we going to copy/paste code into our own IDE from a picture? Commented Oct 24, 2018 at 8:25
  • Yeah that's my bad, first time actually asking a question myself on this site! Commented Oct 24, 2018 at 8:27

2 Answers 2

1

Node.js does not come natively with XHR like browsers. Perhaps you should try using the built in http.request.

There is also a node module called xmlhttprequest that you can use which will make your code work without changing much of it.

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

Comments

0

Welcome to the forum..!

You are getting an error as you are trying to run XMLHttpRequest as an browser style. To run this in nodejs, you need the following package: https://www.npmjs.com/package/xmlhttprequest

Here is the usage that how you can include it in NodeJS. https://www.npmjs.com/package/xmlhttprequest#usage

2 Comments

I just installed the npm xlmhttprequest, but it did not seem to help, so I did this instead: var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; var xhr = new XMLHttpRequest(); which seemed to work, thanks for the help mate !
Great! Enjoy Coding

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.