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

250
Views
Cómo mostrar texto sin formato en el navegador, extrayéndolo de la API

Intenté usar JSON.parse y JSON.stringify, pero no estoy seguro de cómo mostrar texto sin formato sin caracteres especiales en el navegador, usando un botón, obteniendo de una API. Cualquier ayuda o guía sería apreciada. Cuando se hace clic en el botón, quiero obtener de la API una nueva broma aleatoria y mostrarla en el navegador en texto sin formato.

 const fetchDataBtn = document.querySelector("#fetch-data"); const result = document.querySelector("#result"); // gets data from API and sets the content of #result div async function getData() { result.innerText = "Loading...."; try { const res = await fetch("http://api.icndb.com/jokes/random"); const jsonResult = await res.json(); result.innerText = JSON.stringify(jsonResult, null, 2); } catch (error) { console.log(error); } } // add event listener for #fetch-data button fetchDataBtn.addEventListener("click", getData);
 <!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"> <title>ChuckNorrisJokes</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="landing"> <div class="title"> <h1>Chuck Norris Jokes</h1> </div> <button id="fetchdata">Get More Jokes</button> <div id="result"></div> </div> <script src="script.js"></script> </body> </html>

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

0

Solo necesita acceder al valor correcto, no clasificar todo el objeto, el valor de broma ya es una cadena:

 result.innerText = jsonResult.value.joke;
about 4 years ago · Juan Pablo Isaza Report

0

Si lo hice bien, solo quieres mostrar el chiste. En ese caso, debe verificar la estructura json para acceder a los valores de interés. Que en este caso es una broma que está dentro del objeto de valor. Para imprimirlo en el navegador web, puede usar este código.

result.innerText = jsonResult["value"]["joke"]

about 4 years ago · Juan Pablo Isaza Report

0

  1. Primero debe corregir su error: la identificación de su elemento debe ser fetchdata , no fetch-data .

  2. Simplemente acceda al objeto usando la notación de puntos normal. Si observa la respuesta , verá que hay una propiedad de value , y dentro de eso hay una propiedad de joke , y eso es lo que necesita agregar al texto de su elemento.

 const fetchDataBtn = document.querySelector("#fetchdata"); const result = document.querySelector("#result"); // gets data from API and sets the content of #result div async function getData() { result.innerText = "Loading...."; try { const res = await fetch("http://api.icndb.com/jokes/random"); const jsonResult = await res.json(); result.textContent = jsonResult.value.joke; } catch (error) { console.log(error); } } // add event listener for #fetch-data button fetchDataBtn.addEventListener("click", getData);
 <div class="landing"> <div class="title"> <h1>Chuck Norris Jokes</h1> </div> <button id="fetchdata">Get More Jokes</button> <div id="result"></div> </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!