Created
November 25, 2020 13:22
-
-
Save whichbuffer/c6aae02539051bec0a4ca31a6b6fbd30 to your computer and use it in GitHub Desktop.
fetch img data
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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<ul id="imglist"></ul> | |
<script src="script.js"></script> | |
</body> | |
</html> |
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
const list = document.getElementById("imglist") | |
async function Data() { | |
//Fetch the JSON | |
const response = await fetch('https://gist.githubusercontent.com/whichbuffer/c835320d7dcf9899a22890b0a3589d88/raw/97f9bb3d4e0c9d558cc5184eb21a12831895dfa4/IMDB.json'); | |
const data = await response.json(); | |
//Loop over the fetched array | |
for(let i = 0; i < data.Contents.length; i++){ | |
//Create the `<li>` | |
const li = document.createElement("li") | |
//Create the `<p>` for the title and add it to the `<li>` | |
const p = document.createElement("p") | |
p.appendChild(document.createTextNode(data.Contents[i].Title)) | |
li.appendChild(p) | |
//Create the `<img>` for the poster and add it to the `<li>` | |
const img = document.createElement("img") | |
img.src = data.Contents[i].Poster | |
li.appendChild(img) | |
//Add the `<li>` to the list | |
list.appendChild(li) | |
} | |
} | |
Data().catch(error => { | |
console.log("error!"); | |
console.error(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment