承诺解决后无法获得JSON响应

时间:2021-02-28 22:24:43

I'm sorry, you guys. I really hate having to ask the question; I promise, I've been through as many other questions that look even tangentially related as my patience will allow.

对不起,伙计们。我真的不想问这个问题;我保证,我已经经历了许多其他问题,这些问题看起来甚至与我的耐心允许相关。

Following the code from the following questions:

遵循以下问题的代码:

I've got the following:

我有以下内容:

var XHR = [];

//This parses some selections on a screen, which, in turn, informs the URL inside the loop.
$("#list > input:checkbox:checked").each(function(){
    result = [];
    var list = $(this).val();
    XHR.push($.ajax({
        type:'GET',
        url:"https://a-place.com/subsite/webapp/_api/web/lists/getByTitle('"+list+"')/items?$select=" + select + "&$filter=" + filter + "&$expand=" + expand + "&$top=10000",
        dataType: "json",
        headers: {Accept:"application/json;odata=verbose"},
        complete:function(data){}
    }));
});

$.when(XHR).then(function(data){
    console.log(data);
});

No matter what I do, I'm only ever getting the promises inside that .when.then construction. I can't do anything with them, trying to access the responseJSON property, where all the actual objects are, does nothing. I've tried adding a return statement inside the complete callback function, that doesn't appear to change what's actually getting pushed into the XHR array.

无论我做什么,我只会在那里得到承诺。然后施工。我无法对它们做任何事情,试图访问responseJSON属性,其中所有实际对象都没有做任何事情。我已经尝试在完整的回调函数中添加一个return语句,它似乎不会改变实际被推入XHR数组的内容。

Ideally, this is supposed to return a bunch of list items from one or more SharePoint lists that match the selected options and put the matched items into a single array that I can do stuff with.

理想情况下,这应该从一个或多个与所选选项匹配的SharePoint列表中返回一堆列表项,并将匹配的项放入我可以处理的单个数组中。

Edit

Hmm. Ok, based on advice, I've tried:

嗯。好的,根据建议,我试过:

success:function(data){}

And

success:function(data){return data.d.results;}

And nothing really changes in the console regardless of whether I use $.when.then or .done.

无论我使用$ .when.then还是.done,控制台都没有真正改变。

Thanks in advance.

提前致谢。

3 个解决方案

#1


1  

You can try collecting all results in your success callback, and use it afterwards in 'then' function callback:

您可以尝试在成功回调中收集所有结果,然后在'then'函数回调中使用它:

var results = [];
...
success: function(data){
    results.push(data.d.results);
}
...
$.when(XHR).then(function(){
   // Here you should have all results filled in
   console.log(results);
});

#2


0  

Your usage of $.when is incorrect. $.when is not implemented like Promise.all, instead it expects multiple parameters. Fortunately, this is very easy to get around.

您对$ .when的使用不正确。 $ .when没有像Promise.all那样实现,而是需要多个参数。幸运的是,这很容易解决。

$.when.apply($, XHR).then(...

#3


-1  

Instead of using the complete callback you need to set the success callback. The docs says that the success callback has a data argument and also that the complete callback executes after the success and error callbacks.

您需要设置成功回调,而不是使用完整的回调。文档说成功回调有一个数据参数,并且在成功和错误回调之后执行完整的回调。

A sample I have from my code: (the ApplyUpdate function gets called with the json data that is then applied to my existing objects)

我的代码中的示例:(使用json数据调用ApplyUpdate函数,然后将其应用于现有对象)

    function doRefresh(repeat) {
    if (nextTimeOut) clearTimeout(nextTimeOut);
    j$.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: baseUrl + "/WebService/Service.asmx/GetSummary",
        data: "{}",
        dataType: "json",
        success: function (json) { ApplyUpdate(json.d, repeat); }
    });
}

function ApplyUpdate(resp, repeat) {
    goodObj.text(resp.GoodCount);
    errObj.text(resp.BadCount);
    if (repeat) nextTimeOut = setTimeout(autoRefresh, waitTime);
}

#1


1  

You can try collecting all results in your success callback, and use it afterwards in 'then' function callback:

您可以尝试在成功回调中收集所有结果,然后在'then'函数回调中使用它:

var results = [];
...
success: function(data){
    results.push(data.d.results);
}
...
$.when(XHR).then(function(){
   // Here you should have all results filled in
   console.log(results);
});

#2


0  

Your usage of $.when is incorrect. $.when is not implemented like Promise.all, instead it expects multiple parameters. Fortunately, this is very easy to get around.

您对$ .when的使用不正确。 $ .when没有像Promise.all那样实现,而是需要多个参数。幸运的是,这很容易解决。

$.when.apply($, XHR).then(...

#3


-1  

Instead of using the complete callback you need to set the success callback. The docs says that the success callback has a data argument and also that the complete callback executes after the success and error callbacks.

您需要设置成功回调,而不是使用完整的回调。文档说成功回调有一个数据参数,并且在成功和错误回调之后执行完整的回调。

A sample I have from my code: (the ApplyUpdate function gets called with the json data that is then applied to my existing objects)

我的代码中的示例:(使用json数据调用ApplyUpdate函数,然后将其应用于现有对象)

    function doRefresh(repeat) {
    if (nextTimeOut) clearTimeout(nextTimeOut);
    j$.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: baseUrl + "/WebService/Service.asmx/GetSummary",
        data: "{}",
        dataType: "json",
        success: function (json) { ApplyUpdate(json.d, repeat); }
    });
}

function ApplyUpdate(resp, repeat) {
    goodObj.text(resp.GoodCount);
    errObj.text(resp.BadCount);
    if (repeat) nextTimeOut = setTimeout(autoRefresh, waitTime);
}