Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

221
Vistas
I am using getElementById to get data from this API, but it is not returning anything in the table when I run it

Whenever I run the HTML this is what shows up:the table shows up fine but the data from the API site doesn't show up.

Javascript

async function fetchData() {
  const res = await fetch('https://api.covidtracking.com/v1/us/current.json');
  const record = await res.json();
  document.getElementById('date').innerHTML = record.data[0].date;
  document.getElementById('positive').innerHTML = record.data[0].positive;
  document.getElementById('death').innerHTML = record.data[0].death;
  document.getElementById('deathIncrease').innerHTML =
    record.data[0].deathIncrease;
}
fetchData();

HTML

<!DOCTYPE html>
<html>

<head>
    <title>US Covid Cases</title>
    <meta charset="utf-8">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
    <div class="container mt-3" style="width: 450px;">
        <h2 class="text-center">US Covid Cases</h2>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>positives</th>
                    <th>deaths</th>
                    <th>Death increase</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td id="date"></td>
                    <td id="positive"></td>
                    <td id="death"></td>
                    <td id="deathIncrease"></td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
<script type="text/javascript" src="MY PATH"></script>

</html>

Is it a problem that the API URL ends in a .json? I am very new to this type of code. I'm not sure what is wrong in my code, is it a problem with the API website?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

You can try to print out the record object, you will find that the data type is an array.

[
  {
    ...,
    "date": 20210307,
    "positive": 28756489,
    "death": 515151,
    "deathIncrease": 842
  }
]

So just replace record.data[0] with record[0]

async function fetchData() {
  const res = await fetch("https://api.covidtracking.com/v1/us/current.json");
  const record = await res.json();
  document.getElementById("date").innerHTML = record[0].date;
  document.getElementById("positive").innerHTML = record[0].positive;
  document.getElementById("death").innerHTML = record[0].death;
  document.getElementById("deathIncrease").innerHTML = record[0].deathIncrease;
}
fetchData();
<!DOCTYPE html>
<html>

<head>
  <title>US Covid Cases</title>
  <meta charset="utf-8">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
  <div class="container mt-3" style="width: 450px;">
    <h2 class="text-center">US Covid Cases</h2>
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>Date</th>
          <th>positives</th>
          <th>deaths</th>
          <th>Death increase</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td id="date"></td>
          <td id="positive"></td>
          <td id="death"></td>
          <td id="deathIncrease"></td>
        </tr>
      </tbody>
    </table>
  </div>
</body>

</html>

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda