为什么XMLHttpRequest变量变为“未定义”?

时间:2020-04-06 11:53:15

标签: javascript xmlhttprequest

在控制台中运行此代码会产生错误:Cannot read property 'status' of undefined

xhr = new XMLHttpRequest(); // global variable
xhr.open('GET', '/');
xhr.onload = function(){
    console.log(xhr.status); // this line throws the error
    var xhr = 'a'; // local variable
}
xhr.send();

如果我将var xhr = 'a';行注释掉,它将起作用。有人可以解释这里发生的事情吗?

在Chrome和Firefox中进行了测试。

0 个答案:

没有答案