connection_aborted()不能在Ajax调用工作 [英] connection_aborted() does not work on ajax calls

查看:199
本文介绍了connection_aborted()不能在Ajax调用工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDITED

我有一个Ajax调用(使用 $。阿贾克斯() ),它调用下面的PHP脚本。

I've got an ajax call (using $.ajax()) which calls the following php script.

for ($i=0;$i<40;$i++) {
    echo " ";
    flush();
    if (connection_aborted()) {
        log_message('error','CONNECTION IS ABORTED!!!!!');
        exit;
    }
    else {
        log_message('error','connection not aborted :(');
    }
    sleep(1);
}

这也适用于40秒。

如果我关闭它触发调用浏览器窗口, connection_aborted() 仍然返回false,即使我明确地发出一个字符串,并刷新缓冲区!

If I close the browser window which triggered the call, connection_aborted() still returns false, even if I sent explicitly a string and flushed the buffer!

有没有人有答案,请在这里?

Does anyone have an answer here please?

推荐答案

您将需要添加ignore_user_abort(真正的);在上面的PHP脚本,并呼吁使用ob_flush()相呼应,从脚本的东西后,(为什么看到的 PHP冲水()手册页 )。工作示例(概念验证):

You will need to add "ignore_user_abort(true);" on top of the PHP script, and to call "ob_flush()" after echoing something from script (For why see PHP flush() man page). Working example (proof of concept):

<?php

ignore_user_abort(true);

function log_message($s, $ss) {
  $myFile = "log.txt";
  $fh = fopen($myFile, 'a') or die("can't open file");
  $stringData = $s . ": " . $ss . "\n";
  fwrite($fh, $stringData);
  fclose($fh);
}



for ($i=0;$i<5;$i++) {

    echo "<br>";
    //flush();
    ob_flush();

    if (connection_aborted()) {
        log_message('error1', connection_status());
        exit;
    }
    else {
        log_message('error2', connection_status());
    }

    sleep(1);
}

P.S。 CONNECTION_STATUS()返回0,如果连接仍处于活动状态,并且如果封闭其中一个返回1。

P.S. connection_status() returns 0 if connection is still active, and in case of closed one returns 1.

编辑:

我的坏。通话双方的flush()和使用ob_flush()(请从的 这个话题 ),或以其他方式可能无法正常工作,这取决于服务器/ PHP配置。 下面code的测试上WAMP用PHP 5.3.8(工作没有调用flush()),现在在Ubuntu用PHP 5.3.10。其中,刷新()调用之前使用ob_flush()是必要的。

My bad. Call both flush() and ob_flush() (please read flush() man page, link above, and answers from this topic), or otherwise might not work, depending on server/php configuration. The following code was tested on WAMP with PHP 5.3.8 (works without calling flush()), and now on Ubuntu with PHP 5.3.10. where flush() call before ob_flush() is necessary.

全部code来进行测试:

Full code for testing:

index.html的:     

 <html>
  <head>
    <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>

    <script>

      $(document).ready(function() {

        $.ajax({
          url: "script.php",
          context: document.body
        }).done(function(data) { 
         alert(data);
        });

      })

    </script>

  </head>

  <body>
  </body>

</html>

script.php的:     

ignore_user_abort(true);

function log_message($type, $message, $file = 'log.txt') {
    $fh = fopen($file, 'a') or die("can't open file");

    $conn_status = connection_status();

    if($conn_status === CONNECTION_NORMAL) {
        $status = 'normal';
    } elseif($conn_status  === CONNECTION_ABORTED) {
         $status = 'aborted';
    } else {
        $status = 'timeout';
    }

    $aborted = connection_aborted() ? 'yes' : 'no';

    $data  = $type . ': ' . $message . "\n";
    $data .= 'Connection status: ' . $status . "\n";
    $data .= 'Aborted: ' . $aborted . "\n\n\n";

    fwrite($fh, $data);
    fclose($fh);
}



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

    echo "<br>";
    flush();
    ob_flush();

    if (connection_aborted()) {
        log_message('Error', 'Connection closed by user!');
        exit;
    }
    else {
        log_message('Info', 'Everything is fine. Move along...');
    }

    sleep(1);
}

在你打电话的index.html页,并关闭选项卡或整个浏览器,你应该在log.txt文件接下来的信息,请参阅:

After you call index.html page, and close tab or whole browser you should see in log.txt file next info:

Info: Everything is fine. Move along...
Connection status: normal
Aborted: no


Info: Everything is fine. Move along...
Connection status: normal
Aborted: no


Info: Everything is fine. Move along...
Connection status: normal
Aborted: no


Error: Connection closed by user!
Connection status: aborted
Aborted: yes

这篇关于connection_aborted()不能在Ajax调用工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆