发送 AJAX 结果但继续在 PHP 中处理

发布于 2024-10-08 12:49:52 字数 1378 浏览 0 评论 0原文

我正在使用 AJAX 更新数据库中的一些值。一切都工作得很好,但现在我想实现一些日志记录的东西。日志记录功能看起来需要花费相当多的处理时间,并且用户没有理由必须等待它们完成才能看到 AJAX 结果。

因此,我正在尝试找到一种方法来发送 AJAX 结果并仍然在服务器端继续处理。我的研究提出了ignore_user_abort函数,但显然我没有正确使用它。

本指南是我的代码的基础。

这是我的 javascript (Jquery) :

 $.ajax({
            type: "GET",
            url: "ajax.php",
            data: { "mydata": mydata },
            success: function(msg) {
                    $("span#status").fadeOut(200, function() {
                            $("span#status").html(msg);
                            $("span#status").fadeIn(200);
                    });

            }
    });

和我的 PHP:

    $response = "This is my response";

    //Begin code from link
    ob_end_clean();
    header("Connection: close");
    ignore_user_abort(true);
    ob_start();
    echo $response;
    header("Content-Length: " . mb_strlen($response));
    ob_end_flush();
    flush();
    //End code from link

    echo "I should not see this text";

不幸的是,我在flush();之后看到了该文本。

有什么想法吗?

更新 - 已修复:我发现了我的错误。在逐字复制大量不同的代码建议后,我认为这一定是我的 apache/php 配置中的错误。结果我需要添加两行来强制 apache 不缓冲我的结果:

apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);

I'm using AJAX to update some values in a database. All has worked wonderfully with that, but now I would like to implement some logging stuff. The logging functions look like they are going to take a fair amount of processing time, and theirs no reason that the user should have to wait for them to finish to see their AJAX results.

So, I'm trying to find a way to send AJAX results and still continue processing on the server side. My research has brought up the ignore_user_abort function, but apparently I'm not using it correctly.

This guide is what I'm basing my code off.

Here's my javascript (Jquery) :

 $.ajax({
            type: "GET",
            url: "ajax.php",
            data: { "mydata": mydata },
            success: function(msg) {
                    $("span#status").fadeOut(200, function() {
                            $("span#status").html(msg);
                            $("span#status").fadeIn(200);
                    });

            }
    });

And my PHP:

    $response = "This is my response";

    //Begin code from link
    ob_end_clean();
    header("Connection: close");
    ignore_user_abort(true);
    ob_start();
    echo $response;
    header("Content-Length: " . mb_strlen($response));
    ob_end_flush();
    flush();
    //End code from link

    echo "I should not see this text";

Unfortunately, I am seeing that text after the flush();

Any ideas?

Update - Fixed: I figured out my error. After copying word for word tons of different code suggestions, I figured it must have been an error in my apache/php configuration. Turns out I need to add two lines to force apache not to buffer my results:

apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别念他 2024-10-15 12:49:52

PHP 脚本无法告诉浏览器关闭连接而不等待进一步的数据。使用 flush(); 仅将当前输出沿链发送到 Web 服务器,但不能保证它会立即到达浏览器。 Web 服务器可能会缓存输出,直到 PHP 脚本完成执行。

正如OP在最后一段中所写的那样,解决方案是将Apache配置为不缓冲结果。

The PHP script cannot tell the browser to close the connection and not wait for further data. Using flush(); only sends the current output down the chain, to the web server, but it does not guarantee it will arrive to the browser immediately. The web server may cache the output until the PHP script completes the execution.

As the OP wrote in the last paragraph, the solution is to configure Apache to not buffer the results.

樱&纷飞 2024-10-15 12:49:52

好吧,你看到它,不是吗? 你让它回显

严肃地说,你的例子应该有效。代码似乎是在刷新之后执行的,因此如果刷新将请求发送到浏览器,您应该能够进行日志记录。

Well, you would see it, wouldn't you? You told it to echo!

In all seriousness, your example should work. It seems that code is executing after the flush, so you should be able to do your logging if the flush sent the request to the browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文