gpt4 book ai didi

javascript - onload XMLHttpRequest 外部变量

转载 作者:可可西里 更新时间:2023-11-01 16:11:05 26 4
gpt4 key购买 nike

<分区>

我有这个 XMLHttpRequest,我想打印变量 contents... 但是在函数 onload 之外,变量 contents 是“”。如何访问函数外的变量?

var xhr = new XMLHttpRequest();
var contents = ""
xhr.open("GET", fileURL);
xhr.responseType = "arraybuffer";
xhr.onload = function () {
if (this.status === 200) {
var blob = new Blob([xhr.response], {type: "application/pdf"});
var objectUrl = URL.createObjectURL(blob);
alert("sucess")

var reader = new FileReader();
reader.readAsBinaryString(blob);
reader.onload = function(e) {
contents = e.target.result;
}
}
else {
alert("insucess");
}
};
xhr.send();


console.log(contents);

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com