Created
February 27, 2018 01:59
-
-
Save radius/48757b5a02ebac38b2bf2195cd1a4a2d to your computer and use it in GitHub Desktop.
XHR Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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