-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!

halfer
  • 19,824
  • 17
  • 99
  • 186
Anderzen
  • 1
  • 2
  • 2
    Please paste the code / error messages directly instead of supplying an image. Perhaps [this post](https://stackoverflow.com/questions/32604460/xmlhttprequest-module-not-defined-found) will help. – meowgoesthedog Oct 24 '18 at 08:14
  • I guess the examples you read are targeted for browsers and you execute the code in nodejs. – Roland Starke Oct 24 '18 at 08:16
  • It's XML, not CML `let chr = new XMLHttpRequest()` Please check that you wrote the write method name there. – Sami Ahmed Siddiqui Oct 24 '18 at 08: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? – Dennis Lukas Oct 24 '18 at 08:25
  • Yeah that's my bad, first time actually asking a question myself on this site! – Anderzen Oct 24 '18 at 08:27
  • You can (and should) still repair this question. Please take the error and paste in it by editing the question. – halfer Oct 24 '18 at 17:54

2 Answers2

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.

James Chen
  • 45
  • 1
  • 5
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

Sami Ahmed Siddiqui
  • 2,328
  • 1
  • 16
  • 29
  • 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 ! – Anderzen Oct 24 '18 at 08:30
  • Great! Enjoy Coding – Sami Ahmed Siddiqui Oct 24 '18 at 08:50