开发者

xhr.upload.addEventListener load has empty xhr.responseText

开发者 https://www.devze.com 2023-02-09 18:13 出处:网络
xhr.open(\'put\',url,false) xhr.upload.addEventListener(\'load\',function(e){alert(xhr.responseText)},false)
xhr.open('put',url,false)
xhr.upload.addEventListener('load',function(e){alert(xhr.responseText)},false)

Why is xhr.responseText empty? When using xhr.onreadystatechange 4 xh开发者_运维技巧r.responseText has data?


What Gert is trying to say is that you have to add the event listener to the xhr object, not 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)

I needed to set XMLHttpRequest asynchronously for it to work.

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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号