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

148
Visualizações
How to wait unitl xhr finished?

I'm making a website where I need to get all people who are registered to an event but every time I make the xhr request the code execute and do not wait the xrh response, that's cause a lot of trouble in the following code...

let inscrit = new XMLHttpRequest();
let methode = "GET";
let url = "php/getInscrit.php";
let asynchronous = true;

inscrit.open(methode, url, asynchronous);
inscrit.timeout = 4000;
inscrit.onload = function () {
  let people;

  if (this.readyState == 4 && this.status == 200) {
    people = JSON.parse(this.responseText);
  }
};
inscrit.send(null);
return people;

Sorry for my bad English and ty

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It seems like the problem is in "onload", which is only called one time, which could be right at the beginning before it's fully loaded, instead you should do everything in the onreadystatechange callback, so for example:

  let inscrit = new XMLHttpRequest();
    let methode = "GET";
    let url = "php/getInscrit.php"
    let asynchronous = true;

    inscrit.open(methode, url, asynchronous);
    inscrit.timeout = 4000;
    inscrit.onreadystatechange = function() {


        let people ;

        if (this.readyState == 4 && this.status == 200) {

            people = JSON.parse(this.responseText);


        }
    }
    inscrit.send(null);
    return people ;
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