Ajax chiamata - stato di pronto confusione flusso
-
10-10-2019 - |
Domanda
stavo sviluppando con Ajax chiamata e durante il debug, ho trovato l'Ajax sta inviando richiesta / risposta così tanti tempo di quanto in realtà ho immaginato.
Molto tempo fa ho ricevuto un buon documento che descrive quello che succede dietro le quinte, ma ho perso.
Al giorno d'oggi tutorial Ajax sul web parla solo a come codice e quelli IF controlla solo readystate == 4
e status == 200
che non prevede buona spiegazione per uno come me.
ho provato il flusso con il codice qui sotto e uscita è credo strano. La mia confusione è il motivo per cui pronto 4 Stato sta mostrando due volte? Secondo la definizione, pronti 4 attraverso completato quindi non ci dovrebbe essere alcun motivo per completare due volte?
USCITA
START
ready 1 //loading
START
ready 2 //loaded
ready 2 status=200 //loaded
START
ready 3 //interactive
ready 3 status=200 //interactive
START
ready 4 //complete
START
ready 4 //complete ... again???
TEST frammento di codice
xmlHttp.onreadystatechange = function() {
alert("START");
if(xmlHttp.readyState == 0) {
alert('ready 0');
alert('ready 0 status=' + xmlHttp.status);
}
if(xmlHttp.readyState == 1) {
alert('ready 1');
alert('ready 1 status=' + xmlHttp.status);
}
if(xmlHttp.readyState == 2) {
alert('ready 2');
alert('ready 2 status=' + xmlHttp.status);
}
if(xmlHttp.readyState == 3) {
alert('ready 3');
alert('ready 3 status=' + xmlHttp.status);
}
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
alert('ready 4');
}
}
Soluzione
Si può leggere in quirksmode sul modo in cui diversi browser si comportano con le chiamate AJAX e readyState.
questo link che le domande usando il comando Abort
emetterà un readyState 4 (non hanno provato) -? stai usando Abort