简体   繁体   English

xhr.upload.addEventListener加载具有空的xhr.responseText

[英]xhr.upload.addEventListener load has empty xhr.responseText

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

Why is xhr.responseText empty? 为什么xhr.responseText为空? When using xhr.onreadystatechange 4 xhr.responseText has data? 当使用xhr.onreadystatechange 4 xhr.responseText有数据吗?

What Gert is trying to say is that you have to add the event listener to the xhr object, not xhr.upload . Gert想要说的是,您必须将事件侦听器添加到xhr对象,而不是xhr.upload I'm not entirely sure why and the spec isn't exactly clear. 我不确定原因为何, 规格也不完全清楚。 It isn't anything to do with asynchronous requests which are the default anyway. 这与异步请求无关,异步请求仍然是默认设置。

Instead of: 代替:

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

You must do: 您必须做:

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

(My old answer maybe wrong see new answer Timmmm) (我的旧答案可能有误,请参阅新答案Timmmm)

I needed to set XMLHttpRequest asynchronously for it to work. 我需要异步设置XMLHttpRequest才能工作。

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

xhr.addEventListener('load',function(e){alert(xhr.response)},false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM