发送AJAX的结果,但继续处理在PHP继续、结果、AJAX、PHP

2023-09-10 14:28:42 作者:院长放我出院

我使用AJAX来更新数据库中的一些价值观。所有曾与该表现的很出色,但现在我想实现一些日志记录的东西。日志记录功能,看起来他们将采取的处理时间相当多,而且他们没有理由认为用户应该等待他们完成,看看他们的AJAX的结果。

所以,我试图找到一种方法来发送AJAX的结果,仍然继续在服务器端处理。我的研究带来了ignore_user_abort功能,但显然我没有正确地使用它。

本指南就是我立足我的code了。

下面是我的JavaScript(jQuery的):

  $。阿贾克斯({
            键入:GET,
            网址:ajax.php
            数据:{MYDATA:MYDATA},
            成功:函数(MSG){
                    $(#跨度地位)。淡出(200,函数(){
                            $(#跨度状态)HTML(MSG);
                            $(#跨度状态)淡入(200)。
                    });

            }
    });
 
php项目 ajax发送请求到后台之后,后台应该怎么处理返回给回调函数

和我的PHP:

  $回应=这是我的反应;

    //开始从链接code
    ob_end_clean();
    标题(连接:关闭);
    ignore_user_abort(真正的);
    ob_start()函数;
    回声$反应;
    标题(内容长度:mb_strlen($响应)。);
    ob_end_flush()函数;
    冲洗();
    //结束$ C $从路段C

    回声我不应该看到这些文字;
 

不幸的是,我看到的文本冲洗后();

任何想法?

更新 - 修正:我想通了,我的错误。复制字的不同code建议词吨后,我想那一定是在我的Apache / PHP配置错误。原来我需要添加两行中强制Apache不缓存我的结果:

  apache_setenv(无gzip压缩',1);
ini_set('zlib.output_com pression',0);
 

解决方案

PHP脚本不能告诉浏览器关闭连接,而不是等待进一步的数据。使用的flush(); 仅发送电流输出环比下滑,到Web服务器,但它并不能保证它会立即到达浏览器。直到PHP脚本完成执行Web服务器可能缓存输出。

由于OP在最后一段写道,解决的办法是配置Apache没有缓冲的效果。

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

解决方案

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.