Domanda

Sto cercando di rendere il mio script Python trasmetteva il suo output sulla mia pagina web come stampato.

Quindi nel mio JavaScript lo faccio:

var xmlhttp;
var newbody = "";
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==3) {
      newbody = newbody + xmlhttp.responseText;
      document.getElementById("new").innerHTML=newbody;
    }
}
xmlhttp.open("GET","http://localhost/cgi-bin/temp.py",true);
xmlhttp.send();

E nella mia sceneggiatura Python ho:

print "Content-Type: text/plain"
print ""
print " " * 5000   # garbage data for safari/chrome
sys.stdout.flush()

for i in range(0,5):
    time.sleep(.1)
    sys.stdout.write("%i " % i)
    sys.stdout.flush()

Ora mi aspetto 0 1 2 3 4, ma quello che ottengo è 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4

Sembra inviare ogni volta l'intero buffer, quando quello che voglio è per inviare una cifra per onreadystatechange.

Che cosa sto facendo di sbagliato?

È stato utile?

Soluzione

xmlhttp.responseText Sul lato client contiene sempre l'intera risposta, quindi non è necessario newbody, basta usare xmlhttp.responseText.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top