Isabell
asked on
Why arrow function is not working?
Hi,
This following function works fine.
However, when I use an arrow function for xhr.onload, the code is not working.
any idea why? Also how should I fix this issue?
This following function works fine.
function loadData() {
const xhr = new XMLHttpRequest();
xhr.open('get', 'data.txt', true);
xhr.onload = function () {
console.log('ReadyState', this.readyState);
console.log(this.status);
if (this.status == 200) {
document.getElementById('output').innerHTML = `<h1>${this.responseText}</h1>`;
}
}
xhr.send();
}
However, when I use an arrow function for xhr.onload, the code is not working.
any idea why? Also how should I fix this issue?
function loadData() {
const xhr = new XMLHttpRequest();
xhr.open('get', 'data.txt', true);
xhr.onload = () => {
console.log('ReadyState', this.readyState);
console.log(this.status);
if (this.status == 200) {
document.getElementById('output').innerHTML = `<h1>${this.responseText}</h1>`;
}
}
xhr.send();
}
What browser do you use? It might not be supported.
ASKER
Hi zc2. I am using Chrome browser.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks zc2!
You're welcome!