?Briella 2016-08-13 21:01 采纳率: 0%
浏览 32

Ajax调用冗长的PHP任务

I need to execute a long (10-60 sec) PHP task from Ajax. Furthermore, the calling page needs to close the connection and go to a new page, with the PHP task finishing in the background.

At first I tried this scenario:

$.ajax({
   url: '/ff.php',  
   type: 'POST'
}).success(function(response) { 
   console.log("... returned");
});
window.top.location.href = "http://yahoo.com";      

But that never executes ff.php as I thought it should. Somehow it goes to the new page before calling the Ajax... or the connection is closed due to the new page, before it executes. This, despite the precautions I thought I needed in ff.php:

ignore_user_abort(true);
ob_end_clean();
header("Connection: close");
ignore_user_abort();  
ob_start();
echo ('Text the user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();  
flush();             
// Do processing here 

session_write_close();

sleep(30);

require_once($_SERVER['DOCUMENT_ROOT'].'/ajax/lib/sendPush.php');

I've seen numerous posts that that top block of code is what's needed to have the PHP return control to the caller immediately but I can't get it to work in this scenario -- I suspect it's because I'm moving to a new page. That last require statement sends a push notification to my app on my iPhone, which woks just fine. I use this to know that my PHP script has been executed and it will part of my production environment when this works.

I have also tried moving the new page URL into the Ajax success block as follows, since I thought the PHP had all the code necessary to return immediately.

$.ajax({
   url: '/ff.php',  
   type: 'POST'
}).success(function(response) { 
   console.log("... returned");
   window.top.location.href = "http://yahoo.com"; 
});

But in that case, it takes the full 30 secs before going to the next page.

The desired results are: execute the PHP, receive the push right away, and the page goes to the next page quickly. But so far I either get the next page immediately, with NO execution of ff.php or, it does execute but the browser waits the full 30 secs to go to the next page.

UPDATE: As one commenter said below "basically if you redirect before the response then your ajax call will be cancelled." ... so my follow up question is this... is there no way to know when the Ajax has been received and started execution, so I can do the redirect at that time? I looked through the Ajax events and they all seemed to be related exclusively to when execution is complete. Does this imply there is no way to handle a long ajax call via jQuery? We have to use a queue?

  • 写回答

4条回答 默认 最新

  • weixin_33725722 2016-08-13 21:07
    关注

    Try something like this:

    $.ajax({
       url: '/ff.php',  
       type: 'POST'
    }).success(function(response) { 
       console.log("... returned");
    });
    $('#status').text("I'm redirecting you to a new page...");
    setTimeout(function(){window.top.location.href = "http://yahoo.com";},1000);  //Put some delay between ajax and redirect
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮