Skip to content

Instantly share code, notes, and snippets.

@Toinne
Created November 24, 2019 09:36
Show Gist options
  • Save Toinne/c1b9475bd793baf30f6e87590dc22e8a to your computer and use it in GitHub Desktop.
Save Toinne/c1b9475bd793baf30f6e87590dc22e8a to your computer and use it in GitHub Desktop.
Fetch exercise
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<h2><a href="https://aws.random.cat/" target="_blank">a random cat website</a></h2>
<p>this page also has an API to get random cat images, so use it already!</p>
<button id="get-random-cat">load a random cat</button>
<div id="cat-space"></div>
<script>
function getRandomCatHandler() {
// make api request
fetch("https://aws.random.cat/meow", { method: "GET" })
.then(response => response.json())
.then(catData => {
// render data to DOM
/*
do a little dom manipulation to clear the cat-space
and to put in the new image from catData
*/
})
.catch(err => console.log(err))
}
document.getElementById("get-random-cat")
.addEventListener("click", getRandomCatHandler);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment