如何向PHP发送一个简短的Ajax请求,得到确认,不要等待完整的处理完成

first post here :) I have a mobile app (Phonegap) that sends an ajax request to a PHP server, say a friend invite. When receiving the request, a few things have to be done (create links, authorisations, send e-mails, etc) which take a long time (5 sec). During that time, the user on the app is looking at a spinner, waiting for the ajax request to complete.

Ideally:

  • return a confirmation "Friend request received", without waiting for the process to finish.
  • No Cron jobs because waiting 1 min is too long.
  • Something simple (Gearman is scary)

On the app :

$.ajax({
    url: 'http://www.my_site.com/action.php',
    type: 'POST',
    data: "my_guid="+my_guid+"&friend_guid="+friend_guid, 
    dataType: 'json',
    success: function(json) {
        // Send confirmation
    },
    error: function(json) {
        // Show error
    }
}

On the server (PHP):

$my_guid = $_POST['my_guid'];
$friend_guid = $_POST['friend_guid'];
// return here confirmation to the app
veryLongFunction($my_guid,$friend_guid); //continue executing long function
查看全部
dotxxh0998
dotxxh0998
2015/07/29 13:16
  • fork
  • php
  • ajax
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复