如何合并两个jQuery AJAX调用的结果并使用deferred / promise

时间:2016-03-29 21:55:14

标签: javascript jquery ajax

我试图合并两个jQuery AJAX调用的结果。我在这里看了其他类似的问题,但似乎都没有帮助。对于每个ajax调用(其中2个),我成功调用函数createStatusView并将结果传递给它。好的部分是结果适用于两个AJAX调用。坏消息是我的$ .when调用为res1和res2返回undefined,然后将结果的undefined值发送到我的createStatusView。所以,我真的不想为两个AJAX调用调用createStatusView,只需$ .when。任何建议都将不胜感激。

function getSpecifiedList(listName, userId){

    var url = SP.PageContextInfo.get_webServerRelativeUrl() + "/_vti_bin/listdata.svc/" + listName;

    var url1 = url + "?$select=ParentOrg,ORG,URL,Site_Status&$inlinecount=allpages";       
    var call1 = $.ajax({
        url: url1,
        type: "GET",
        headers:{"accept":"application/json;odata=verbose",
        },
        success: function(results){createStatusView(results, listName);},
        error:function(error){
            console.log("Error in getting List: " + listName);
            $(_options.container).html("Error retrieving your " + listName + ". " + 
            SP.PageContextInfo.get_webServerRelativeUrl());
        }
    });

    var url2 = url + "?$select=ParentOrg,ORG,URL,Site_Status&$inlinecount=allpages&$skiptoken=1000";
    var call2 = $.ajax({
        url: url2,
        type: "GET",
        headers:{"accept":"application/json;odata=verbose",
        },
        success: function(results){createStatusView(results, listName);},
        error:function(error){
            console.log("Error in getting List: " + listName);
            $(_options.container).html("Error retrieving your " + listName + ". " + 
            SP.PageContextInfo.get_webServerRelativeUrl());
        }
    });
    //the res1 and res2 come back undefined
            //call1 and call2 are objects as shown in F12 debug
    $.when(call1,call2).done(function(res1,res2){
        var results = res1[0].d.results.concat(res2[0].d.results);
        createStatusView(results,listName);
    });
}

2 个答案:

答案 0 :(得分:1)

尝试在$ .when回调函数中添加一些对象检测。它会将未定义的值保留在列表中。

$.when(call1,call2).done(function(res1,res2){
    var results = res1[0].d.results.concat(res2[0].d.results);
    if (results) {
        createStatusView(results,listName);
    }
});

另一个例子:

$.when(call1,call2).done(function(res1,res2){
   if (!res1 && !res2) return;
   var results = res1[0].d.results.concat(res2[0].d.results);
   if (results) {
      createStatusView(results,listName);
   }
});

与此相同:

$.when(call1,call2).done(function(res1,res2){
   if (res1 && res2) {
      var results = res1[0].d.results.concat(res2[0].d.results);
      if (results) {
         createStatusView(results,listName);
      }
   }
});

答案 1 :(得分:0)

您是否尝试将ajax调用移动到单独的函数,然后从when?

调用它们
function getSpecifiedList(listName, userId){
    $.when(call1(listName),call2(listName)).then(function(res1,res2){
        var results = res1[0].d.results.concat(res2[0].d.results);
        createStatusView(results,listName);
    });
}

function call1(listName){   
    var url = SP.PageContextInfo.get_webServerRelativeUrl() + "/_vti_bin/listdata.svc/" + listName;
    var url1 = url + "?$select=ParentOrg,ORG,URL,Site_Status&$inlinecount=allpages";   

    return $.ajax({
        url: url1,
        type: "GET",
        headers:{
            "accept":"application/json;odata=verbose",
        }
    }); 
};

function call2(listName){
    var url = SP.PageContextInfo.get_webServerRelativeUrl() + "/_vti_bin/listdata.svc/" + listName;
    var url2 = url + "?$select=ParentOrg,ORG,URL,Site_Status&$inlinecount=allpages&$skiptoken=1000";
    return $.ajax({
        url: url2,
        type: "GET",
        headers:{
            "accept":"application/json;odata=verbose",
        }
    });
};