0

Here is my code:

var request = new XMLHttpRequest();

request.open("GET", "data.json", true); // not working

request.onload = function() {
  // begin accessing JSON data here
  var data = JSON.parse(this.response);

  for (var i = 0; i < data.length; i++) {
    console.log(data[i].name + " is a " + data[i].race + ".");
  }
};

request.send();

I run node json.js and it stops right at the first line and gives me XMLHttpRequest is not defined. What could I be doing wrong?

Blue
  • 22,608
  • 7
  • 62
  • 92
Doug Lamar
  • 23
  • 3
  • 1
    Possible duplicate of [XMLHttpRequest module not defined/found](https://stackoverflow.com/questions/32604460/xmlhttprequest-module-not-defined-found) – sam Feb 03 '19 at 02:44

1 Answers1

0

XMLHttpRequest is a Web API. You need to use the node.js framework, which defines the http API which you can use for web requests.

There are also plenty of other frameworks that can help you out:

Blue
  • 22,608
  • 7
  • 62
  • 92