Created
August 7, 2017 20:35
-
-
Save prof3ssorSt3v3/92736919fcc46ee8b8a6d497a0c4ef64 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//fetch user data from jsonplaceholder | |
//generate a user list on the web page | |
//add a click event to the body that will | |
//refresh the list each time with a random | |
//number of users | |
const uri = 'http://jsonplaceholder.typicode.com/users'; | |
let req = new Request(uri, { | |
method: 'GET', | |
mode: 'cors' | |
}); | |
fetch(req) | |
.then( (response)=>{ | |
if(response.ok){ | |
return response.json(); | |
}else{ | |
throw new Error('BAD HTTP!'); | |
} | |
}) | |
.then( (jsonData) =>{ | |
//console.log(jsonData); | |
let ul = document.querySelector('#users'); | |
let df = new DocumentFragment(); | |
jsonData.forEach( (user) =>{ | |
let li = document.createElement('li'); | |
let pn = document.createElement('p'); | |
let pue = document.createElement('p'); | |
pn.textContent = user.name; | |
pue.textContent = ''.concat(user.username, ' - ', user.email); | |
pn.className = 'name'; | |
pue.classList.add('info'); | |
li.appendChild(pn); | |
li.appendChild(pue); | |
df.appendChild(li); | |
}); | |
ul.appendChild(df); | |
}) | |
.catch( (err) =>{ | |
console.log('ERROR:', err.message); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment