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

111
Visualizações
Is there any ES6 equivalent for ajax call?

in vanillaJS we are doing something like that to ajax call :

var request = new XMLHttpRequest();
request.open('GET','file.json',true);
request.onload = function (){
if(this.status >= 200 && this.status<400){
 var data = JSON.parse(this.response);
 console.log(data);
}
else{console.log("Error !");
}
request.send();

Now if we do this with arrow Functions it won't work because of this keyword.

Is there anyway to solve this problem and use it with arrow function or should I just stick with old way?

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

0

In short, there is no an "ES6 equivalent" for XMLHttpRequest (not even fetch) simply because none of them is an ECMAScript feature but part of the Web platform API defined by WHATWG and W3C.

However, to rewrite your code in a more "modern" way, using some ES6 features, you can procede as follows:

const request = new XMLHttpRequest();

request.addEventListener('load', (e) => {
  // YOU COULD ALSO ACCESS request AS e.target HERE.  

  if (request.status >= 200 && request.status < 400) {
    const data = JSON.parse(request.response);
    console.log(data);
  } else {
    console.log('ERROR');
  }
});

request.open('GET', 'file.json');
request.send();

Essentially we are using addEventListener API (which is still not an ES feature, but the latest specification to handle events in browsers). Then we are using const and an arrow function as handler which are ES6 features.

You don't need to use this as you can access your request through the event object or leveraging the reference stored in request variable.

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