Skip to content

Instantly share code, notes, and snippets.

@radius
Created February 27, 2018 01:59
Show Gist options
  • Save radius/48757b5a02ebac38b2bf2195cd1a4a2d to your computer and use it in GitHub Desktop.
Save radius/48757b5a02ebac38b2bf2195cd1a4a2d to your computer and use it in GitHub Desktop.
XHR Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simple XHR Example</title>
<script type="text/javascript">
function reqListener () {
var jsonResponse = JSON.parse(this.responseText);
var characters = jsonResponse.data.results;
for(var i=0; i<characters.length; i++) {
var character = characters[i];
if(character.thumbnail.path.indexOf('image_not_available') === -1) {
var image = character.thumbnail.path + '/portrait_incredible.' + character.thumbnail.extension;
var img = document.createElement('img');
img.src = image;
document.body.appendChild(img);
}
}
}
// create an XHR object
var oReq = new XMLHttpRequest();
// add a 'load' listener to the object
oReq.addEventListener("load", reqListener);
// open the request
oReq.open("GET", "https://gateway.marvel.com/v1/public/characters?apikey=SIGN_UP_FOR_YOUR_OWN_DAMN_KEY&limit=100");
// send the request
oReq.send();
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment