When I use console.log(actual_JSON)
I actually see my array of objects but if I return it from within a function like return actual_JSON
I have an undefined array. What is happening? This is my js:
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'userinfo.json', true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(xobj.responseText);
}
};
xobj.send(null);
}
function init() {
alert("hi");
loadJSON(function (response) {
var actual_JSON = JSON.parse(response);
console.log(actual_JSON);
})
}
My JSON file looks like:
[
{
"username": "lovelyday",
"email": "lovelyday@gmail.com"
},
{
"username": "what",
"email": "whoknows@nothing.com"
}
]