jQuery中的多个同时Ajax请求(带有一个回调)

时间:2016-04-01 10:25:07

标签: javascript php jquery ajax jquery-deferred

我正在尝试通过将所有数据库调用拆分为单独的脚本并使用ajax同时运行它来更快地加载页面。我做了一些研究,发现jQuery有一个$ .when()。then()函数可以解决这个问题。我对ajax的基础知识很好,但延迟/承诺概念让我有点困惑。我尝试了下面的代码,但它只会加载第一个ajax结果中的数据。注释掉第一个将加载下一个,但只有一个将显示在页面上。 感谢任何帮助。感谢。

 $.when(  $.post('extensions/ext_adjustments_investigation/general_details.php', {sku: sku},function(data) { result = data; }),  
                $.post('extensions/ext_adjustments_investigation/location_information.php', {sku: sku},function(data1) { result1 = data1; }),
                $.post('extensions/ext_adjustments_investigation/annotations.php', {sku: sku},function(data2) { result2 = data2; }),
                $.post('extensions/ext_adjustments_investigation/adjustment_history.php', {sku: sku},function(data3) { result3 = data3; })

    ).then( 
    function() {
        $("#searching").addClass("hidden");
        $("#load").prop("disabled",false);  
                $("#general").html(result).hide().slideDown()("slow");
                $("#location").html(result1).hide().slideDown()("slow");
                $("#anno").html(result2).hide().slideDown()("slow");
                $("#history").html(result3).hide().slideDown()("slow");
                }
    );  

3 个答案:

答案 0 :(得分:1)

这可以机械化如下:

var baseURL = 'extensions/ext_adjustments_investigation/';
var data = [
    {script: 'general_details.php', selector: "#general"},
    {script: 'location_information.php', selector: "#location"},
    {script: 'annotations.php', selector: "#anno"},
    {script: 'adjustment_history.php', selector: "#history"}
];
var promises = data.map(function(item) {
    return $.post(baseURL + item.script, {sku: sku}).then(function(data, textStatus, jqXHR) {
        return data;
    }, function(error) {
        console.log(error);
        return $.when('error');//error recovery
    });
});
$.when.apply(null, promises).then(function() {
    $('#searching').addClass('hidden');
    $('#load').prop('disabled', false);
    for(var i=0; i<arguments.length; i++) {
        $(data[i].selector).html(arguments[i]).hide().slideDown()('slow');
    }
});

答案 1 :(得分:0)

$.when(calla(), callb()).then(
    function(result1, result2){
        //do something with the results
    }
    ,
    function(error){
        console.log('error'+error);
    }
);

您继续在成功和错误函数中添加参数以使其工作。希望它有所帮助

答案 2 :(得分:0)

也许你需要从一个回调中获取数据

$.when(
    $.post('extensions/ext_adjustments_investigation/general_details.php', {sku: sku}),
    $.post('extensions/ext_adjustments_investigation/location_information.php', {sku: sku}),
    $.post('extensions/ext_adjustments_investigation/annotations.php', {sku: sku}),
    $.post('extensions/ext_adjustments_investigation/adjustment_history.php', {sku: sku})
).then(
    function (general, location, annotations, history) {
        $("#searching").addClass("hidden");
        $("#load").prop("disabled", false);
        $("#general").html(general).hide().slideDown()("slow");
        $("#location").html(location).hide().slideDown()("slow");
        $("#anno").html(annotations).hide().slideDown()("slow");
        $("#history").html(history).hide().slideDown()("slow");
    }
);