As stated in the title, I want to import the content of a .json file in JavaScript as a JavaScript object, using a XMLHttpRequest.
I am able to import it and, for example, log the content of the .json to the console in the request.onload = function(), but I don't know how to save it as a local object in my code.
I tried a lot of ways, (e.g. see code below), but it doesn't work.
var requestURL = *url*;
var request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'json';
request.send();
var GAMESTATE = {};
request.onload = function(GAMESTATE) {
GAMESTATE = request.response;
}
console.log(GAMESTATE);
The log just says "undefined" in the console.
The .json-content is the following:
{
"PAUSED": 0,
"RUNNING": 1,
"MENU": 2,
"GAMEOVER": 3,
"NEWLEVEL": 4
}
Thanks for helping! :)