I have this XMLHttpRequest and I want to print the variable contents
... But outside function onload
the variable contents
is "". How I can access the variable outside the function?
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);