php - 在 PHP 中响应 ajax 请求后如何继续处理?

标签 php ajax timeout

有些操作耗时过长,导致ajax请求超时。

如何先完成对请求的响应,然后继续该操作?

最佳答案

ignore_user_abort指令,和 ignore_user_abort函数可能是您正在寻找的:它应该允许您将响应发送到浏览器,然后,仍然在您的服务器上运行一些计算。

这篇关于它的文章可能会让您感兴趣:如何使用 ignore_user_abort() 进行带外处理;引用:
编辑 2010-03-22: 删除了链接 (指向 http://!waynepan.com/2007/10/11/!how-to -use-ignore_user_abort-to-do-process-out-of-band/ -- 删除空格和 ! 如果你想尝试 ),在看到评论后来自@Joel。

Basically, when you use ignore_user_abort(true) in your php script, the script will continue running even if the user pressed the esc or stop on his browser. How do you use this?
One use would be to return content to the user and allow the connection to be closed while processing things that don’t require user interaction.

The following example sends out $response to the user, closing the connection (making the browser’s spinner/loading bar stop), and then executes do_function_that_takes_five_mins();

给出的例子:

ignore_user_abort(true);
header("Connection: close");
header("Content-Length: " . mb_strlen($response));
echo $response;
flush();
do_function_that_takes_five_mins();

(还有很多我没有复制粘贴)


请注意,您的 PHP 脚本仍然必须符合 max_execution_timememory_limit 约束 - 这意味着您不应该将其用于需要太多时间。

这也将使用一个 Apache 进程——这意味着您不应该有几十个页面同时执行此操作。

我想,这仍然是增强使用体验的好方法 ;-)

关于php - 在 PHP 中响应 ajax 请求后如何继续处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1481247/

相关文章:

php - 使用 jquery 与 php 类交互的最佳实践?

javascript - codeandtheory 的实时搜索如何运作?

javascript - AJAX 调用 Twitter 时出现 400 错误

php - MySQL/PHP : group by classes

php - 基于变量 MYSQL IPN 的 substr 问题

php - 如何将从 url 获取的变量分配给表(HTML、PHP)

javascript - ajax调用后div重新加载错误

node.js - 需要减少expressjs中路由的超时时间

asp.net - 身份验证超时无法正常工作

jenkins pipeline选项超时然后通知