嗨,我只是想知道为什么我的JavaScript重定向在我的PHP响应后立即无法工作?这是场景,1.我正在向我的PHP脚本发送帖子请求。2.我的PHP脚本然后对此进行处理并返回字符串" OK"以识别该过程已完成(但是我的脚本正在执行一个背景过程,而后期过程则花费太长)。3.如果字符串还可以,我将把用户重定向到确认页面。但是,在返回"确定"字符串后,我的重定向不会立即生效,似乎仍在等待PHP过程完成。以下是我的代码
<?php
$success = $payment->do_payment();
if (!$success) {
throw new Exception("error on payment");
}
set_time_limit(0);
ignore_user_abort(true);
header("Connection: closern");
header("Content-Encoding: utf-8rn");
ob_start();
echo "ok"; // This return to the frontend immediately
$size = ob_get_length();
header("Content-Length: {$size}", true);
ob_end_flush();
ob_flush();
flush();
// Below this line is a long php process that takes almost a minute
$this->veryLongProcessRunningOnBackground();
这是我的jQuery请求
function payment_do_payment(token){
$.ajax({
url: '/payment',
data:{
mydata: "The data"
},
success: function(txt){
if (txt == "ok"){
console.log("done redirecting now");
window.location.href = "/confirmation-page";
}else{
alert("Failed to process");
}
},
type: 'POST'
});
}
您需要使用套接字连接之类的东西来执行此操作。
也许这篇文章会有所帮助。