jQuery支持Transfer-Encoding:chunked [英] jQuery support Transfer-Encoding:chunked
问题描述
我是一名网络开发人员. 在我的脚本中,使用header()设置"Transfer-Encoding:chunked".并冲洗()到网页. 它将分时在网页上打印. 它工作正常. 但是,当我使用jQuery.ajax()请求此操作时,它总是一起输出(分块没用).
i'm a web developer. in my script use header() to set "Transfer-Encoding:chunked". and flush() to webpage. it will print in webpage time-shared. it works ok. but, when i use jQuery.ajax() to request this.it always output all together(chunked unuseful).
如何解决这个问题?在jQuery ajax中使用分块编码?
how to solute this? use chunked encoding in jQuery ajax?
推荐答案
您不能使用jquery.ajax连续读取分块的HTTP响应. jQuery ajax仅在连接终止时才调用成功回调函数.你应该用 此 jquery插件.
you cannot use jquery.ajax to read chunked http response continuously. jquery ajax will call the success callback function only when connection terminates. You should use this jquery plugin.
如果您使用的是php,则可以使用以下代码:
if you are using php then you can use this code:
<html>
<head>
<script src="jquery-1.4.4.js"></script>
<script src="jquery.stream-1.2.js"></script>
<script>
var println = function(string){
$("#console").append(string+"<br />");
}
$(document).ready(function(){
$.stream("stream.php",{
open:function(){
println("opened");
},
message:function(event){
println(event.data);
},
error:function(){
println("error");
},
close:function(){
println("closed");
}
});
});
</script>
</head>
<body>
<div id="console"></div>
</body>
</html>
在服务器端:
stream.php
stream.php
<?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);
}
?>
这篇关于jQuery支持Transfer-Encoding:chunked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!