在之前的一次项目中,需要用户上传数据,我们计算之后将结果以邮件的形式返回。遇到问题就是ajax调用PHP脚本之后,使用echo json_encode(上传成功信息)之后,一直需要等到计算完毕脚本才输出,这样一直很恼火的弄了半天。
后来在查阅Stack Overflow之后,问题得到解决,解决方案如下
....
echo json_encode($result);
// get the size of the output
$size = ob_get_length();
// 告诉浏览器关闭连接
header("Content-Length: $size");
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
ignore_user_abort(true);
set_time_limit(0); //no time limit
/******** 之后需要运行的代码********/
Stack Overflow的原文章连接如下:英文区连接
英语不难,大家也可以去多多提问