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)
精彩评论