你不能使用
jquery.ajax连续读取chunked http响应.只有在连接终止时,jquery ajax才会调用成功回调函数.你应该用
this jquery插件.
如果您使用的是php,那么您可以使用以下代码:
var println = function(string){
$("#console").append(string+"
");
}
$(document).ready(function(){
$.stream("stream.php",{
open:function(){
println("opened");
},
message:function(event){
println(event.data);
},
error:function(){
println("error");
},
close:function(){
println("closed");
}
});
});
在服务器端:
stream.php
header('Content-Encoding', 'chunked');
header('Transfer-Encoding', 'chunked');
header('Content-Type', 'text/html');
header('Connection', 'keep-alive');
ob_flush();
flush();
echo("23123454645645646;");
$p = "";
for ($i=0; $i < 1024; $i++) {
$p .= " ";
};
echo($p.";");
for ($i = 0; $i < 10000; $i++) {
echo('6;string;');
ob_flush();
flush();
sleep(2);
}
?>