我使用jquery的ajax来调用php api:
$.ajax({
type:"POST",
dataType:"json",
data:{type:type,fid:fid,model:'star'},
url:'Recommend/Star/mark',
success:function (data) {
//do something
},
...
});
Recommend/Star/mark方法中的PHP代码:
error_reporting(0);
set_time_limit(0);
ob_implicit_flush(true);
ob_end_clean();
ob_start ();
echo json_encode($boolMark);
ob_flush();
flush();
sleep(3);
$this->afterMark($uid, $fid);
我想立即向ajax发送php echo结果,但我的代码不起作用。
如何让php立即将结果发送到ajax,并继续执行剩余的代码?
发布于 2018-08-21 15:01:05
你希望你的代码在发送响应后在“后台”运行,为此你需要这样做:
// Destroy the current buffer (Just in case...)
ob_clean();
// Start a new buffer
ob_start();
// Send your response.
echo json_encode($boolMark);
// Disable compression (in case content length is compressed).
header("Content-Encoding: none");
// Set the content length of the response.
header("Content-Length: ".ob_get_length());
// Close the connection.
header("Connection: close");
// Flush all output.
ob_end_flush();
ob_flush();
flush();
这会发送完整的标头并关闭连接,否则你的连接将保持活动状态,浏览器将等待php脚本终止其执行。
https://stackoverflow.com/questions/-100002323
复制相似问题