2011-01-28 73 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