Skip to content

Instantly share code, notes, and snippets.

@SomeoneWeird
Last active August 29, 2015 14:06
Show Gist options
  • Save SomeoneWeird/747091dc6ce32f197ee2 to your computer and use it in GitHub Desktop.
Save SomeoneWeird/747091dc6ce32f197ee2 to your computer and use it in GitHub Desktop.
//Takes a url to JSON data and returns an object
function req(url, cb) {
var r = new XMLHttpRequest();
r.open('GET', url, true);
r.onload = function() {
if(this.status != 200) return cb(new Error("Expected 200 got " + this.status));
try {
var data = JSON.parse(this.response)
} catch(e) {
return cb(new Error("Error parsing JSON:" + e.toString()));
}
cb(null, data);
}
r.onerror = function() {
cb(new Error("Error requesting " + url));
}
r.send();
}
req('./data.json', function(err, data) {
if(err) {
return console.error("There was an error requesting data:", err);
}
[ 'title_popular', 'title_exact', 'title_substring' ].forEach(function(title) {
var t = data[title];
for(var i = 0; i < t.length; i++) {
console.log("Title of item: " + t[i].title);
console.log("Description of item: " + t[i].description);
console.log("ID of item: " + t[i].id);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment