weixin_33709609 2015-01-08 08:37 采纳率: 0%
浏览 8

用ajax睡觉

I have a index.php file as below:

    ob_end_clean();
    header("Connection: close");
    ignore_user_abort(true); // optional
    ob_start();
    for($i=1; $i<100; $i++) {
      echo $i . "."; echo ('Text the user will see'); echo '<br>';
    }
    $size = ob_get_length();
    header("Content-Length: $size");
    ob_end_flush();     // Will not work
    flush(); 

    // At this point, the browser has closed connection to the web server

    // Do processing here
    sleep(20);
    file_put_contents ('test.txt', 'test', FILE_APPEND);

If i run on browser localhost/index.php its work, because it can show 'text the user will see' on screen and no need wait 20 seconds. But if i call it with ajax like as below:

$.ajax({
     url: "index.php"
});

It cant show 'text the user will see' for me. And it is work after waited 20 seconds. How can i fix them. Thank you

  • 写回答

1条回答 默认 最新

  • 关注

    I just tried your code using this jquery code in all major browsers and all worked, they closed the connection and displayed result:

    $.ajax({
          url: "index.php"
    }).done(function(data) {
       $('#remote').html(data);
    });
    
    评论

报告相同问题?