Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
How do I make multiple API calls via XMLHttpRequest?

I'm not that experienced when it comes to APIs so excuse me in advance.

I'm trying to get a button to display the contents (response) of an API, and the code below works wonders. I'm wondering how I would make the exact same request, but instead of the "explicit" endpoint, I want a "history" endpoint instead.

How would I do this?

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 Respostas
Responde à pergunta

0

The simplest answer would be to make it a function:

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);
}

And then you can you call it like this:

send("explicit");

The only problem you then face is that object1 is not available outside of the function. You could work around this by making object1 a global variable, or do the things you want to directly in the EventListener.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda