Hi I'm just wondering why is it that my javascript redirect doesn't work immediately after my php respond? Here's the scenario, 1. I'm sending a post request to my php scripts. 2. My php scripts then process this and return a string "ok" to identify that the process is done (but my script is doing a background process which takes too long). 3. If the string is ok I will redirect the user to a confirmation page. But after returning the "ok" string my redirect don't take effect immediately it seems that its still waiting for the PHP process to finish. Below is my code
<?php
$success = $payment->do_payment();
if (!$success) {
throw new Exception("error on payment");
}
set_time_limit(0);
ignore_user_abort(true);
header("Connection: close\r\n");
header("Content-Encoding: utf-8\r\n");
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();
and this is my jquery request
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'
});
}
You will need to use something like a socket connection to do this.
Maybe this post will help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.