Skip to content

Instantly share code, notes, and snippets.

@mazell
Created February 6, 2014 13:18
Show Gist options
  • Select an option

  • Save mazell/8843945 to your computer and use it in GitHub Desktop.

Select an option

Save mazell/8843945 to your computer and use it in GitHub Desktop.
JS: LoadJson
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'file.json', true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// .open will NOT return a value but simply returns undefined in async mode so use a callback
callback(xobj.responseText);
}
}
xobj.send(null);
}
// Call to function with anonymous callback
loadJSON(function(response) {
// Do Something with the response e.g.
//jsonresponse = JSON.parse(response);
// Assuming json data is wrapped in square brackets as Drew suggests
//console.log(jsonresponse[0].name);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment