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

187
Views
¿Cómo realizo varias llamadas a la API a través de XMLHttpRequest?

No tengo tanta experiencia en lo que respecta a las API, así que disculpe de antemano.

Estoy tratando de obtener un botón para mostrar el contenido (respuesta) de una API, y el código a continuación funciona de maravilla. Me pregunto cómo haría exactamente la misma solicitud, pero en lugar del punto final "explícito", quiero un punto final de "historial".

¿Cómo haría esto?

 const data = null; const xhr = new XMLHttpRequest(); xhr.withCredentials = true; var object1; xhr.addEventListener("readystatechange", function () { if (this.readyState === this.DONE) { object1 = JSON.parse(this.responseText); } document.getElementById('thejoke').innerHTML = object1.value; }); xhr.open("GET", "https://api.chucknorris.io/jokes/random?category=explicit"); xhr.send(data);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La respuesta más simple sería convertirlo en una función:

 function send(category) { const url = "https://api.chucknorris.io/jokes/random?category=" + category const data = null; const xhr = new XMLHttpRequest(); xhr.withCredentials = true; var object1; xhr.addEventListener("readystatechange", function () { if (this.readyState === this.DONE) { object1 = JSON.parse(this.responseText); } document.getElementById('thejoke').innerHTML = object1.value; }); xhr.open("GET", ""); xhr.send(data); }

Y luego puedes llamarlo así:

 send("explicit");

El único problema al que te enfrentas es que object1 no está disponible fuera de la función. Puede solucionar esto haciendo que object1 una variable global, o hacer las cosas que desea directamente en EventListener .

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!