[英]XMLHttpRequest send() from local file returns undefined with Workers
當我發出請求時,通過XMLHttpRequest()
send會返回null
,但是onload
方法返回的是我調用send時期望的文件。 我究竟做錯了什么? 這是代碼:
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()
實際上的返回值為undefined
。
在您的代碼中,您嘗試通過執行以下操作為集合分配請求的值:
let resultado = requisicao.send();
這顯然會給變量賦undefined
。
您使用xhr.onload
就是我也會做的事情,我真的不明白為什么您要有兩個返回方法。
當您使xhr不是異步的時,您可以執行以下操作:
requisciao.send();
return requisciao.responseText;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.