When I am using an arrow function in xhr.onload
, I don't get a response. However, if I use a normal function like xhr.onload = function(){}
then I get the response. Why does the code before not work like it does when using xhr.onload = function(){}
?
let btn = document.getElementById("button");
btn.addEventListener("click" , function(){
let xhr = new XMLHttpRequest();
xhr.open("GET" , "sample.txt" , true);
xhr.onload = () => {
if(this.status === 200){
console.log(this.responseText);
}
}
xhr.send();
})