jQuery support Transfer-Encoding:chunked

五迷三道 提交于 2019-11-28 08:46:12

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.

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>

in the server side :

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




?>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!