Having some trouble manipulating data from a local JSON file using this technique (no jQuery) as I can't seem to access the object on a global scale:
var actual_JSON;
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'my_data.json', true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(xobj.responseText);
}
};
xobj.send(null);
}
The function is called after the document loads, using an anonymous function like so:
function loaded(e) {
loadJSON( function(response) {
actual_JSON = JSON.parse(response);
console.log(actual_JSON); // Object {...}
}
);
console.log(actual_JSON); // undefined
};
document.addEventListener("DOMContentLoaded", loaded, false);
I'm struggling with understanding closures and how they impact the scope of actual_JSON
. It seems to be limited to the anonymous function within loadJSON()
, despite my global declaration.