有些操作耗时过长,导致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 executesdo_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_time
和 memory_limit
约束 - 这意味着您不应该将其用于需要太多的操作强>时间。
这也将使用一个 Apache 进程——这意味着您不应该有几十个页面同时执行此操作。
我想,这仍然是增强使用体验的好方法 ;-)
关于php - 在 PHP 中响应 ajax 请求后如何继续处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1481247/