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).
我是一名web开发人员。在我的脚本中,使用header()设置“传输编码:分块”。网页和冲洗()。它将在网页上分时打印。它工作好。但是,当我使用jQuery.ajax()请求时。它总是输出所有的数据(分块无用)。
how to solute this? use chunked encoding in jQuery ajax?
如何解决呢?在jQuery ajax中使用分段编码?
1 个解决方案
#1
11
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.
你不能使用jquery。ajax不断读取块化的http响应。jquery ajax只在连接终止时调用success回调函数。您应该使用这个jquery插件。
if you are using php then you can use this code:
如果您正在使用php,那么您可以使用以下代码:
<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>
in the server side :
在服务器端:
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);
}
?>
#1
11
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.
你不能使用jquery。ajax不断读取块化的http响应。jquery ajax只在连接终止时调用success回调函数。您应该使用这个jquery插件。
if you are using php then you can use this code:
如果您正在使用php,那么您可以使用以下代码:
<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>
in the server side :
在服务器端:
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);
}
?>