【发布时间】:2016-01-06 06:13:38
【问题描述】:
对不起,你们。我真的很讨厌不得不问这个问题;我保证,在我的耐心允许的范围内,我已经解决了尽可能多的其他看似无关紧要的问题。
按照以下问题的代码:
- jQuery when each is completed, trigger function
- jQuery AJAX solution inside each() loop
- How do I return the response from an asynchronous call?
我有以下内容:
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);
});
无论我做什么,我都只会在 .when.then 构造中得到承诺。我无法对它们做任何事情,试图访问所有实际对象所在的 responseJSON 属性,什么也不做。我尝试在完整的回调函数中添加一个 return 语句,这似乎并没有改变实际推送到 XHR 数组中的内容。
理想情况下,这应该从一个或多个与所选选项匹配的 SharePoint 列表中返回一堆列表项,并将匹配的项放入我可以处理的单个数组中。
编辑
嗯。好的,根据建议,我试过了:
success:function(data){}
和
success:function(data){return data.d.results;}
无论我使用 $.when.then 还是 .done,控制台中都没有真正改变。
提前致谢。
【问题讨论】:
-
想想也许你想要
.done(...)而不是.then(...) -
感谢您的建议,但我已经尝试过了。完全相同的结果。
-
不要在成功回调中返回结果,而是尝试将其推送到数组中。
result.push(data.d.results);然后在“then”函数中你会知道你已经准备好了所有结果,你可以使用它进行操作。这里不是花哨的解决方案,但应该可以。 -
太棒了,@Borys,成功了。我可以处理这个。你应该把它写下来作为答案,这样我就可以标记它。谢谢!