Last active
August 15, 2018 03:39
-
-
Save djp424/a9d234a0d14853cd2e422749914447bb to your computer and use it in GitHub Desktop.
Fetch and append JSON data from list of URLs (in order just in case some of the requests are large)
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
// Get's list of urls, grabs the title in json, and appends them to a selector of your choice | |
function requester(urls, selector) { | |
if(urls === undefined || urls.length === 0) { | |
return; | |
} | |
var request = new XMLHttpRequest(); | |
request.open('GET', urls[0]); | |
request.responseType = 'json'; | |
request.onload = function() { | |
if(request.readyState === 4 && request.status === 200) { | |
for(var i in request.response) { | |
var node = document.createElement('li'); | |
var textnode = document.createTextNode(request.response[i]['title']['rendered']); // Based on temp.json | |
node.appendChild(textnode); | |
selector.appendChild(node); | |
} | |
// Take first item out and send another request. | |
urls.shift(); | |
requester(urls, selector); | |
} | |
}; | |
request.send(); | |
} | |
var urls = ['temp.json', 'temp.json', 'temp.json', 'temp.json', 'temp.json']; | |
var resultsDiv = document.querySelector('#results'); | |
requester(urls, resultsDiv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment