Send AJAX results but continue processing in PHP(发送 AJAX 结果但在 PHP 中继续处理)
问题描述
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.
这篇关于发送 AJAX 结果但在 PHP 中继续处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:发送 AJAX 结果但在 PHP 中继续处理
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Laravel 仓库 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01