Javascript xhr.upload.addEventListener加载的xhr.responseText为空

Javascript xhr.upload.addEventListener加载的xhr.responseText为空,javascript,google-chrome,Javascript,Google Chrome,为什么xhr.responseText为空?使用xhr.onreadystatechange 4时xhr.responseText有数据?(我的旧答案可能错误,请参阅新答案Timmmm) 我需要异步设置XMLHttpRequest,它才能工作 xhr.open('put',url,false) xhr.upload.addEventListener('load',function(e){alert(xhr.responseText)},false) (我的旧答案可能错误,请参阅新答案Timmm

为什么xhr.responseText为空?使用xhr.onreadystatechange 4时xhr.responseText有数据?

(我的旧答案可能错误,请参阅新答案Timmmm)

我需要异步设置XMLHttpRequest,它才能工作

xhr.open('put',url,false)
xhr.upload.addEventListener('load',function(e){alert(xhr.responseText)},false)
(我的旧答案可能错误,请参阅新答案Timmm)

我需要异步设置XMLHttpRequest,它才能工作

xhr.open('put',url,false)
xhr.upload.addEventListener('load',function(e){alert(xhr.responseText)},false)

Gert想说的是,您必须将事件侦听器添加到
xhr
对象,而不是
xhr.upload
。我不完全确定原因,也不完全清楚。这与默认的异步请求无关

而不是:

xhr.open('put',url,true)

xhr.addEventListener('load',function(e){alert(xhr.response)},false)
你必须做到:

xhr.upload.addEventListener('load', function(e){ alert(xhr.responseText); }, false);

Gert想说的是,您必须将事件侦听器添加到
xhr
对象,而不是
xhr.upload
。我不完全确定原因,也不完全清楚。这与默认的异步请求无关

而不是:

xhr.open('put',url,true)

xhr.addEventListener('load',function(e){alert(xhr.response)},false)
你必须做到:

xhr.upload.addEventListener('load', function(e){ alert(xhr.responseText); }, false);

lol我实际上没有注意到我将事件侦听器放在了xhr:)上,但如果我记得正确的话xhr.open('put',url,false)也不起作用。进行了小编辑并接受了这个答案,而不是我以前的答案,谢谢。lol我实际上没有注意到我将事件侦听器放在了xhr:)上,但如果我记得正确的话xhr.open('put',url,false)也不起作用。进行了小编辑并接受了此答案,而不是我的旧答案,谢谢。