2011-01-28 71 views
1

我想就根據我的Ajax調用我可以在ajax響應中創建一個jquery ajax請求嗎?

像這樣

$.post('test1.php', function(res) { 

    var processid = res.processid; 

    $.post('test2.php', id : processid, function (data){ 

     // do some stuff and make other ajax calls 
    }); 

},'json') 

這是正確的響應數Ajax調用?我需要根據每個請求的回覆做出其他請求。

謝謝

+1

這應該工作得很好。 – 2011-01-28 18:27:41

回答

1

是的,這是正確的。第二張POST將在第一張完成後運行。

或者,您可以使用jQuery的$.queue$.dequeue以及使用$.data來存儲變量。你也可以使用AJAX Queue插件(Google for them),我不使用任何插件,所以我不能提供任何插件。

//Add POST 1 
$(document).queue('AJAX', function(next){ 
    $.post('test1.php', function(res){ 
     $(document).data('processid', res.processid); //We need to store this somewhere 
     next(); //Call next queued function (if any) 
    }, 'json'); 
}); 

//Add POST 2 
$(document).queue('AJAX', function(next){ 
    $.post('test2.php', 'id='+$(document).data('processid'), function (data){ 
    next(); //Call next queued function (if any) 
    }); 
}); 

// Start the queue 
$(document).dequeue('AJAX'); 
+0

感謝您的快速回復。有沒有更好的方法來做到這一點?或任何庫/插件將排隊我的請求 – supa 2011-01-28 18:34:26