Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
I am trying to show my data obtained from my API but it is not shown I am consuming it with JS

I am trying to show my data obtained from my API but it is not shown I am using JS.

fetch (" http://127.0.0.1:8000/api/vehiculo/ ") .then (res => res.json ()) .then (data => muestraData (data))

const muestraData = (data) => {
   let body = '' 
   for (let i = 0; i <data.length; i ++) {
    body + = <tr><td>${data[i].id}</td></tr> 
   }
   document.getElementById('data').innerHTML = body
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First of all, I don't know how your API response is look like, so in this explanation, I will use (Rick-and-Morty APIs).

https://rickandmortyapi.com/api/character

Let's make a function to fetch the data from API:

const loadData = () => {
  return fetch("https://rickandmortyapi.com/api/character")
    .then((response) => response.json())
    .catch((error) => {
      throw error;
    });
};

Now we can call loadData function and build the body:

const data = document.getElementById("data");

loadData().then((response) => {
  const results = response.results;
  let body = "";

  if (results && results.length > 0) {
    results.forEach((item) => {
      body += `ID: ${item.id} <br/>`;
    });
  }

  data.innerHTML = body;
});

Because the results variable contains an array of objects, we can just use the .forEach method.

loadData returns Promise

Working example:

https://codesandbox.io/s/affectionate-forest-utmr3

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda