0
I make the request normally, but when trying to recover the response of this request there is no return
let rqs = new XMLHttpRequest();
rqs.open("GET", "https://servicodados.ibge.gov.br/api/v1/localidades/distritos");
rqs.send();
let dados = rqs.responseText;
console.log(dados);
Ajax is asynchronous, which means that when line 4 is executed, the request hasn’t even left your computer ( the answer will only arrive a few milliseconds later) and therefore the rqs.responseText variable is null (or undefined). Hence the use of callback
onreadystatechange
, for your script to be warned of when the answer comes.– user201467