3

Python スクリプトの出力を、印刷された Web ページにストリーミングしようとしています。

したがって、私のjavascriptでは次のようにします。

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();

私のPythonスクリプトには次のものがあります:

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()

今私は期待0 1 2 3 4していますが、私が得るのは0 0 1 0 1 2 0 1 2 3 0 1 2 3 4

私が本当に欲しいのは、onreadystatechangeごとに1桁を送信することである場合、毎回バッファ全体を送信しているようです。

私は何を間違っていますか?

4

1 に答える 1

3

xmlhttp.responseTextクライアント側では常に応答全体が含まれているため、 は必要なくnewbody、 を使用するだけxmlhttp.responseTextです。

于 2011-06-13T19:24:40.920 に答える