I am learning Ajax and ES6, so i want to bind a function to an onload property from the XmlHttpRequest obj. At first, i write an anoynymous function and it runs OK. But when i changed the function to arrow syntax, the element with id "text" did not have any value.
//this code runs normally
xhr.onload = function() {
if (this.status === 200) {
document.getElementById("text").innerText = this.responseText;
} else if (this.status === 404) {
document.getElementById("text").innerText = "Not Found!";
}
};
// this code can not run
xhr.onload = () => {
if (this.status === 200) {
document.getElementById("text").innerText = this.responseText;
} else if (this.status === 404) {
document.getElementById("text").innerText = "Not Found!";
}
};
I expect my element with id "text" to have value, but when i changed my function into arrow syntax, the element dont receive any value at all.