I have the following function
function getSetting(item) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", "settings.json", true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
var data = JSON.parse(rawFile.responseText);
return data.item;
}
}
rawFile.send(null);
}
which I am testing like this
console.log(getSetting('restaurant_name'));
It is returning undefined
a console.log of data returns the JSON object of my JSON file, as expected.
a console.log of item returns restaurant_name, as expected.
I must be missing something obvious, but having looked at the other topics on Stackoverflow related to this I haven't come across anything yet.
I have tried using data[0].item to get the result but it also made no difference.
The contents of the settings.json file:
{
"restaurant_name": "Example_Restaurant_Name",
"restaurant_address": "Example Restaurant, England"
}
Help much appreciated in advance, thanks.