readystatechange
is an event, so I think I can register using addEventListener
, but I think most Ajax samples use the method of replacing it with readystatechange
.
Is there any reason to avoid it?Because it's a bit long?
var xhr = new XMLHttpRequest();
xhr.open("GET", "/path/to/file", true);
xhr.onreadystatechange=function(){
if(this.readyState===4&this.status===200){
console.log(xhr.responseText);
}
};
xhr.addEventListener('readystatechange', function(){
if(this.readyState===4&this.status===200){
console.log(xhr.responseText);
}
});
xhr.send("");
(I don't know what it is right now, but) The addEventListener doesn't seem to work with Opera.
According to the W3C specification, the readystatechange needs to be ignited, but it doesn't seem to follow it.
I had a crazy question↓
© 2024 OneMinuteCode. All rights reserved.