+: consulta a api con javascript

This commit is contained in:
jp.dev 2022-08-17 13:07:52 -04:00
parent 25b2974ae6
commit 5afded3719
3 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,97 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="witdth=device-witdth, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Rick & Morty</title>
</head>
<body>
<h1>Personajes</h1>
<main>
<!-- <article>
<div class="image-container">
<img src="https://rickandmortyapi.com/api/character/avatar/243.jpeg" alt="Personaje"</img>
</div>
<h2>Personajes</h2>
<span >Estado de Personajes</span>
</article>
<article>
<div class="image-container">
<img src="https://rickandmortyapi.com/api/character/avatar/243.jpeg" alt="Personaje"</img>
</div>
<h2>Personajes</h2>
<span >Estado de Personajes</span>
</article>
<article>
<div class="image-container">
<img src="https://rickandmortyapi.com/api/character/avatar/243.jpeg" alt="Personaje"</img>
</div>
<h2>Personajes</h2>
<span >Estado de Personajes</span>
</article>
<article>
<div class="image-container">
<img src="https://rickandmortyapi.com/api/character/avatar/243.jpeg" alt="Personaje"</img>
</div>
<h2>Personajes</h2>
<span >Estado de Personajes</span>
</article>
<article>
<div class="image-container">
<img src="https://rickandmortyapi.com/api/character/avatar/243.jpeg" alt="Personaje"</img>
</div>
<h2>Personajes</h2>
<span >Estado de Personajes</span>
</article>
<article>
<div class="image-container">
<img src="https://rickandmortyapi.com/api/character/avatar/243.jpeg" alt="Personaje"</img>
</div>
<h2>Personajes</h2>
<span >Estado de Personajes</span>
</article>
<article>
<div class="image-container">
<img src="https://rickandmortyapi.com/api/character/avatar/243.jpeg" alt="Personaje"</img>
</div>
<h2>Personajes</h2>
<span >Estado de Personajes</span>
</article>
<article>
<div class="image-container">
<img src="https://rickandmortyapi.com/api/character/avatar/243.jpeg" alt="Personaje"</img>
</div>
<h2>Personajes</h2>
<span >Estado de Personajes</span>
</article>
<article>
<div class="image-container">
<img src="https://rickandmortyapi.com/api/character/avatar/243.jpeg" alt="Personaje"</img>
</div>
<h2>Personajes</h2>
<span >Estado de Personajes</span>
</article>
<article>
<div class="image-container">
<img src="https://rickandmortyapi.com/api/character/avatar/243.jpeg" alt="Personaje"</img>
</div>
<h2>Personajes</h2>
<span >Estado de Personajes</span>
</article>
-->
</main>
<script src="index.js"></script>
</body>
</html>

28
js_get_data_api/index.js Normal file
View File

@ -0,0 +1,28 @@
function getCharacters(done) {
const results = fetch("https://rickandmortyapi.com/api/character");
results
.then(response => response.json())
.then(data => {
done(data)
});
}
getCharacters(data => {
console.log(data);
data.results.forEach(personaje => {
const article = document.createRange().createContextualFragment(
/*html*/`
<article>
<div class="image-container">
<a href="https://gitea.kickto.net/jp.av.dev/Intro_basicos_HTML_CSS/js_get_data_api" target="_blank">
<img src="${personaje.image}" alt="Personaje"</img>
</a>
</div>
<h2>${personaje.name}</h2>
<span>${personaje.status}</span>
</article>
`);
const main = document.querySelector("main")
main.append(article)
});
});

28
js_get_data_api/style.css Normal file
View File

@ -0,0 +1,28 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
h1 {
text-align: center;
}
main {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 18px;
}
.image-container {
display: flex;
}
.image-container img {
width: 100%;
}
article {
padding: 10px;
box-shadow: 0px 0px 1px #0a789d;
}