下面的代码通过要追加的ajax内容加载:
$.get('http://mydomain.com/ajax1.php/', function(data){
var data_one = $(data).find('#ajax_editor_suggestion_container');
$('.ajax_editor_selection_container').append(data_one);
});
$.get('http://mydomain.com/ajax2.php/', function(data){
var data_one = $(data).find('#ajax_editor_suggestion_container');
$('.ajax_editor_selection_container').append(data_one);
}); 我的目标是先添加ajax1.php的内容,然后再追加ajax2.php的内容。但是,有时ajax2.php的内容会在ajax1.php的内容之前加载,并会扰乱附加的顺序。
因此,我的问题是,如何确保ajax2.php的内容总是追加在ajax1.php的内容之后?
我使用$.get()是因为它对我来说更容易,但我对任何其他jQuery ajax方法都是开放的。
谢谢
发布于 2014-04-09 15:21:08
我会用$.when
$.when(
$.get('http://mydomain.com/ajax1.php/'),
$.get('http://mydomain.com/ajax2.php/')
).done(function (response1, response2) {
var data_one = $(response1).find('#ajax_editor_suggestion_container');
var data_two = $(response2).find('#ajax_editor_suggestion_container');
$('.ajax_editor_selection_container').append(data_one).append(data_two);
});这将同时运行ajax请求,但在两者都完成之前不会调用回调。
https://stackoverflow.com/questions/22966970
复制相似问题