dongpao9437 2011-06-27 04:06
浏览 33
已采纳

用ajax刷新问题

I used flush() function in infinite loop in my php page, it echoes a text each second. when i open the page in browser it works! but when i load it via jquery ajax it doesn't response!

php page

    <?php

if (ob_get_level() == 0) ob_start();

for ($i = 0; true/*$i<10*/; $i++){

        echo "<br> Line to show. $i";
        echo str_pad('',4096)."
";    

        ob_flush();
        flush();
        sleep(1);

}

ob_end_flush();

?>

jquery code

$.ajax({
  url: 'res.php',

  beforeSend: function(  ) {
    $('#mydiv').html('loading...');
  },
  success: function( data ) {

      $('#mydiv').html(  data );

  }
});
  • 写回答

2条回答 默认 最新

  • dongzhila3786 2011-06-27 04:24
    关注

    HTTP-Streaming can not be done simply using $.get

    insert <script> tags as following: http://ajaxpatterns.org/archive/HTTP_Streaming.php.

     <?
          while (true) {
        ?>
            <script type="text/javascript">
              $('news').innerHTML = '<?= getLatestNews() ?>';
            </script>
        <?
            flush(); // Ensure the Javascript tag is written out immediately
            sleep(10);
          }
        ?>
    

    HTTP-streaming is a very complicated hack. You should consider using long-polling instead which works in every browser. It seems there are some solutions (slide 54)

    Simple long-polling example topic:

    How do I implement basic "Long Polling"?

    This video shows how to do long-polling: http://www.screenr.com/SNH
    

    P.S: this will kill your(bad performance) server for sure. You should have a look at http://pusherapp.com which is free for small sites.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao