Created
November 9, 2023 23:13
-
-
Save ermogenes/3ed05eedd553cba6cf90af5d0d845e49 to your computer and use it in GitHub Desktop.
Aula 09/11/2023 - Fetch API
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
{ | |
"escola": "Etec Adolpho Berezin", | |
"curso": "Informática", | |
"componente": "Programação de Computadores II", | |
"professores": [ | |
"Ermogenes", | |
"Nicholas" | |
] | |
} |
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 http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Aula Fetch</title> | |
</head> | |
<body> | |
<h1>Aula Fetch</h1> | |
<input type="button" value="Carregar" id="carregar"> | |
<p id="mensagem"></p> | |
<ul id="professores"></ul> | |
<script src="index.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
function iniciar() { | |
const botaoCarregar = document.getElementById("carregar"); | |
botaoCarregar.addEventListener('click', carregar); | |
} | |
async function carregar() { | |
// const url = "dados.json"; | |
const url = "https://gist.githubusercontent.com/ermogenes/b28d49289db135bb5b5f9b0b31f1ba02/raw/657141e8f326352a44338654ebf1f456216070b5/dados.json"; | |
const resposta = await fetch(url); | |
const dados = await resposta.json(); | |
const mensagem = `Estou estudando ${dados.componente} no curso de ${dados.curso} da ${dados.escola}.`; | |
exibir(mensagem, dados.professores); | |
} | |
function exibir(mensagem, professores) { | |
const paragrafoMensagem = document.getElementById("mensagem"); | |
paragrafoMensagem.innerText = mensagem; | |
const listaProfs = document.getElementById("professores"); | |
for(const prof of professores) { | |
listaProfs.insertAdjacentHTML('beforeend', `<li>${prof}</li>`); | |
} | |
} | |
document.addEventListener('DOMContentLoaded', iniciar); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment