I got a problem by handling with workers, when i make my request the XMLHttpRequest()
send returns null
, but the onload
method returns the file which i expected when i call send. What am I doing wrong?
Here's the code:
function pegarDados(){
let requisicao = new XMLHttpRequest();
requisicao.overrideMimeType('text/plain');
requisicao.setRequestHeader('X-Requested-With', 'JSONHttpRequest');
requisicao.open('GET','./assets/data/dados.json', false);
requisicao.onload = function(){
return JSON.parse(this.response);
};
let resultado = requisicao.send();
return resultado.responseText;
}
self.onmessage = function (event) {
if(event.data == 'lista'){
let dados = pegarDados();
console.error(dados);
self.postMessage(dados);
}
}
XMLHttpRequest.send()
actually has a return value of undefined
.
In your code, you are trying to assign the set the value of your request by doing:
let resultado = requisicao.send();
which would obviously assign undefined
to the variable.
What you were doing with xhr.onload
is what I would do too, I don't really see why you want to have two return methods.
Looking as you made your xhr not asynchronous, you could just do:
requisciao.send();
return requisciao.responseText;