Consumo uma API que lista os dados de uma raça canina numa tabela. Ao clicar na raça sou direcionado a uma nova página recebendo a id e exibindo as informações do JSON.
Preciso exibir a imagem do cão da API. Não consegui acessar o jpg contido na img: url em https://api.thedogapi.com/v1/breeds
meu objetivo: exibir a imagem no lugar da tag "p" do último código
function fazGet(url){
let request = new XMLHttpRequest()
request.open("GET",url,false)
request.send()
return request.responseText
}
fetch('https://api.thedogapi.com/v1/breeds').then(response => {
return response.json()
})
function criaLinha(usuario){
linha = document.createElement('tr')
tdNome = document.createElement('td')
tdNome.setAttribute('innerText', `https://api.thedogapi.com/v1/breeds/${usuario.id}`)
tdLife = document.createElement('td')
tdMais = document.createElement('a')
tdMais.setAttribute('href',`detalhes.html?id=${usuario.id}`)
tdNome.innerHTML = usuario.name;
tdLife.innerHTML = usuario.life_span;
tdMais.innerHTML = 'Info';
linha.appendChild(tdNome)
linha.appendChild(tdLife)
linha.appendChild(tdMais)
return linha
}
let cont = 0;
function main(){
let data = fazGet(`https://api.thedogapi.com/v1/breeds?limit=25&page=${cont}&order=ASC`)
let usuarios = JSON.parse(data)
let tabela = document.getElementById('tabela')
usuarios.forEach(element => {
let linha = criaLinha(element)
tabela.appendChild(linha)
})
cont++
}
main()
table, th, td, tr{
border: 1px solid black ;
align-items: center;
}
nav{
margin: 15px 15px;
}
<!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">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="main.css">
<title>Document</title>
</head>
<body>
<table id='tabela' >
</table>
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item">
<a class="page-link" href='#' aria-label="Next" onclick="main()">
<span aria-hidden="true">»</span>
<span >Exibir mais</span>
</a>
</li>
</ul>
</nav>
<script src="main.js" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</body>
</html>
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id');
function exec(){
const data = fetch(`https://api.thedogapi.com/v1/breeds/${id}`)
.then(e=>e.json())
.then(param=>{
const dados = param
document.querySelector("#name").innerHTML = dados.name;
document.querySelector("#weight").innerHTML = (`${dados.weight.metric} kg`);
document.querySelector("#life_span").innerHTML = dados.life_span
document.querySelector("#temperament").innerHTML = dados.temperament;
document.querySelector("#origin").innerHTML = dados.origin;
document.querySelector("#image").innerHTML = dados.reference_image_id;
console.log(dados);
})
}
exec()
<!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">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="main.css">
<title>Document</title>
</head>
<body>
<div>
<p>Raça</p>
<span id="name"></span>
<p>Peso</p>
<span id="weight"></span>
<p>Tempo de Vida</p>
<span id="life_span"></span>
<p>Temperamento</p>
<span id="temperament"></span>
<p>Origem</p>
<span id="origin"></span>
<!-- imagem de referência -->
<p id="image">
</p>
</div>
<a class="btn btn-outline-primary" href="index.html" role="button">Retornar para lista</a>
<script src="maindetalhes.js" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</body>
</html>