jquery - 当所有ajax请求完成后,

标签 jquery ajax jquery-deferred

我需要执行一堆 ajax 请求(我需要这样做来填充一些选择框),并在加载所有内容后隐藏加载面板。

如果我这样做,效果会很完美:

$.when(
    $.getJSON(applicationUrl + "data/codiciPaese.json", function (json) {
        data.codiciPaese = json;
        $.each(data.codiciPaese, function (index, item) {
            $(".codiciPaese").append($('<option>', {
                value: item.code,
                text: item.code + " - " + item.name
            }));
        });
    }),
    $.getJSON(applicationUrl + "data/regimiFiscali.json", function (json) {
        data.regimiFiscali = json;
        $.each(data.regimiFiscali, function (index, item) {
            $(".regimiFiscali").append($('<option>', {
                value: item.code,
                text: item.code + " - " + item.name
            }));
        });
    }),
    $.Deferred(function (deferred) {
        $(deferred.resolve);
    })
).done(function () {
    $('#loading').fadeOut(800, function () {
        // something else
    });
});

但是,由于我想采用 DRY 原则:)并且由于将有两个以上的请求,因此我尝试将 getJSON 请求包装到每个循环中:

var elementsToGet = { // I'll reuse this to get the values elsewhere
    codiciPaese: "HY",
    regimiFiscali: "RF01",
};

$.when(
    $.each(elementsToGet, function (element, defaultValue) {
        $.getJSON(applicationUrl + "data/"+element+".json", function (json) {
            data.element = json;
            $.each(data.element, function (thisIndex, thisElement) {
                $(parent + " ."+element).append($('<option>', {
                    value: thisElement.code,
                    text: thisElement.code + " - " + thisElement.name
                }));
            });
        });
    }),
    $.Deferred(function (deferred) {
        $(deferred.resolve);
    })
).done(function () {
    $('#loading').fadeOut(800, function () {
        // something else
    });
});

问题是这种方式不再使用“延迟”,因此加载面板在加载所有数据之前就消失了。我做错了什么?

最佳答案

我认为你可以向 $.when 提供多个 promise :

var promises = $.map(elementsToGet, function(element, defaultValue) {
        return $.getJSON(applicationUrl + "data/" + element + ".json", function(json) {
            $.each(data.element, function(thisIndex, thisElement) {
                $(parent + " ." + element).append($('<option>', {
                    value: thisElement.code,
                    text: thisElement.code + " - " + thisElement.name
                }));
            });
        });
    });

$.when.apply(null, promises).done(function() {
    $('#loading').fadeOut(800, function() {
        // something else
    });
});

注意,.apply(null, Promise) 在这里是因为 $.when 期望延迟对象以 $.when( d1, d2, d3 ) 而不是数组。

关于jquery - 当所有ajax请求完成后,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27444351/

相关文章:

javascript - 如何通过 ajax 将对象数组从 javascript 传递到 asp.net-mvc 函数?

javascript - 减少代码行

php - ajax 不响应所有数组数据

jQuery 成功/错误函数

javascript - API设计: How can I combine result of two deferred jQuery objects?

javascript - 如何解决 for 循环中的所有 promise ?

javascript - jQuery 动态 div id 与 .each() 不起作用

jquery - 使用 jquery ajax 调用模拟表单提交

javascript - 我的网站的 Java 表单验证

jQuery.Deferred() - "new"运算符是可选的吗?