var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function(){
console.log('state changed',ajax.readyState, ajax.status)
}
console.log(ajax);
ajax.open('GET','https://randomuser.me/api/?format=json',true);
ajax.send('');
This is generating status change 4 times. Why 4 times?
Ok so it doesn’t give final state, but whole causality of event. I know those states BTW.
igor_g
September 12, 2020, 5:57pm
4
Event onreadystatechange means: in moment when state changed . If you would to handle AJAX response, you should to check readyState in your event handler…
if (ajax.readyState === 4) {
// response processing
if (ajax.status === 200) {
// handle success
} else {
// handle error
}
}
1 Like
m3g4p0p
September 13, 2020, 6:03pm
5
BTW if you’re only interested in the success state anyway you might just listen to the load event that will only fire once…
1 Like
system
Closed
December 14, 2020, 1:03am
6
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.