Skip to content

Instantly share code, notes, and snippets.

@moisescastillo
Created August 13, 2020 03:25
Show Gist options
  • Save moisescastillo/ef7c4bdb02cacfb2be3962bd4129cdf3 to your computer and use it in GitHub Desktop.
Save moisescastillo/ef7c4bdb02cacfb2be3962bd4129cdf3 to your computer and use it in GitHub Desktop.
Example JSON request
var requestJSON = 'https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json';
let request = new XMLHttpRequest();
request.open('GET', requestJSON);
request.responseType = 'json';
request.send();
request.onload = function() {
let data = request.response;
let members = data.members;
members.forEach(function(value, index) {
console.log(`Name: ${value.name}`);
console.log(`Age: ${value.age}`);
console.log(`Secret Identity: ${value.secretIdentity}`);
if (value.powers.length > 0) {
console.log(`Powers:`);
for (let i in value.powers) {
console.log(i, value.powers[i]);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment