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

164
Views
Reemplace los resultados de la búsqueda de API en la ventana del navegador con los resultados de una nueva llamada (JavaScript)

Tengo tres funciones similares que llaman a datos JSON desde una API usando botones HTML y los cambian a una cadena usando stringify. Los resultados se muestran en un div, y cada nueva llamada se agrega a la página actual del navegador, haciéndola más larga. Me gustaría que cada nueva llamada reemplace los datos anteriores que se muestran en la página.

Si lo entiendo correctamente, no puedo usar ReplaceChild una vez que el JSON está en cadena. He intentado usar string.replace(), pero tal vez lo esté usando incorrectamente.

Aquí está el código de trabajo que estoy tratando de construir:

 <body> <button onclick="awardees()">View NDNP Awardees</button> <button onclick="ocrFeed()">View Recently-Added OCR'd Pages</button> <button onclick="batches()">View Available Batches</button> <script> function awardees(){ fetch('https://chroniclingamerica.loc.gov/awardees.json') .then(function(response) { return response.json(); }) .then(function(myJson) { var string = JSON.stringify(myJson); document.getElementById('results').innerHTML += "<br/>"+string; }); } function ocrFeed(){ fetch('https://chroniclingamerica.loc.gov/ocr.json') .then(function(response) { return response.json(); }) .then(function(myJson) { var string = JSON.stringify(myJson); document.getElementById('results').innerHTML += "<br/>"+string; }); } function batches(){ fetch('https://chroniclingamerica.loc.gov/batches.json') .then(function(response) { return response.json(); }) .then(function(myJson) { var string = JSON.stringify(myJson); document.getElementById('results').innerHTML += "<br/>"+string; }); } </script> <div id ="ticket"> <span id="results"></span> </div> </body> </html>

¿Dónde necesitaría agregar líneas adicionales y qué método cree que sería mejor?

¡Muchas gracias!

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

0

Si entiendo correctamente, solo debe reemplazar document.getElementById('results').innerHTML += "<br/>"+string; por document.getElementById('results').innerHTML = "<br/>"+string; y reemplazará el contenido div con una nueva cadena en lugar de agregarlo al existente.

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!