Using XMLHttpRequest() with WebStorm

I am new to WebStorm and currently coding using ES6.  Whenever I run 'XMLHttpRequest' in my code it comes back with 'XMLHttpRequest is not defined' in the run console.  I expect that I do not have my project setup correctly or I am missing something else.  I wrote the code below which loads data from a JSON file as an example of my issue.  Any assistance on what I am doing wrong is appreciated.

let rooms = {};
let xhr = new XMLHttpRequest();

// Use xhr to load the JSON file.
xhr.open("GET", "data.json", true);

xhr.responseType = 'text';

// Create 'onload callback function to handle file loading
xhr.onload = event => {
// Check that file has loaded properly.
if (xhr.status === 200) {
rooms = JSON.parse(xhr.responseText);
console.log("JSON data loaded.");
console.log(rooms.livingRoom.contents);
if (rooms.closet.light.on === false) {
console.log("The closet light is off.");
}
}
};

// Send request to load the file
xhr.send();

I wrote the code below as an example of my issue

0
1 comment

Seems you run your code with Node.js...XMLHttpRequest is a built-in object in web browsers. It is not available in Node.js; you have to install it separately using npm. Node comes with the http module which has to be used for making HTTP requests from Node

Please either make sure to install and use the modules above, or include your code in HTML page (as inline <script> tag, or via <script src="path/to/your/js.js">) and run it using Run/Debug in .html file right-click menu

0

Please sign in to leave a comment.