Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
Corrija la API del formulario de obtención de datos e imprímala en HTML con JavaScript

Quiero obtener datos de la API y mostrarlos en HTML por elementID con JavaScript. Por favor, ayuda con lo que me equivoco en este código a continuación.

 <div id="priceChange"></div> <script> setInterval(tradestime, 5000); function tradestime(){ fetch("https://api.binance.com/api/v1/ticker/24hr?symbol=LTCUSDT") .then( (res) => { const data = res.json(); } ) document.getElementById("priceChange").innerHTML = data.PriceChange; } </script>

Corrija este código.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. Debe usar res.json() porque desea datos en formato JSON . que devolverá la promise y luego podrá obtener data . Deberías leer fetch docs

     fetch("https://api.binance.com/api/v1/ticker/24hr?symbol=LTCUSDT") .then(res => res.json()) .then(data => el.innerHTML = data.priceChange)
  2. la clave debe ser priceChange no PriceChange .

  3. Puede usar parseFloat + toFixed como:

     el.innerHTML = Number.parseFloat(data.priceChange).toFixed(2); 

 setInterval(tradestime, 5000); const el = document.getElementById("priceChange"); function tradestime() { fetch("https://api.binance.com/api/v1/ticker/24hr?symbol=LTCUSDT") .then(res => res.json()) .then(data => el.innerHTML = data.priceChange) }
 <div id="priceChange"></div>


También puede usar async-await aquí

 setInterval( tradestime, 5000 ); const el = document.getElementById( "priceChange" ); async function tradestime() { const res = await fetch( "https://api.binance.com/api/v1/ticker/24hr?symbol=LTCUSDT" ) const data = await res.json(); el.innerHTML = data.priceChange; }
 <div id="priceChange"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!