我想从 SharePoint 列表中检索所有项目。该列表有超过 5000 个元素,因此需要对 REST-API 进行递归 ajax 调用。我可以将所有数据调用到一个数组中,但是调用 Ajax 函数的代码似乎没有执行任何操作,请参阅下面的代码。我似乎错过了显而易见的事情。
.then()
我得到以下输出:
var url = _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('LISTNAME')/items?$select=ID,Title,TEST,LINK,Modified,Editor/Title&$top=5000&$expand=Editor/Id";
function getAllItemsFromList(url) {
return new Promise((resolve, reject) => {
$.ajax({
url: url,
method: "GET",
dataType: "json",
headers: {
"Accept": "application/json; odata=verbose"
},
}).done(function (data) {
if (data.d.__next) {
console.log("true: ", data.d.results);
url = data.d.__next;
data.d.results.forEach(element => {
all_items_from_list.push(element);
});
getAllItemsFromList(url);
}
else {
console.log("false: ", data.d.results);
data.d.results.forEach(element => {
all_items_from_list.push(element);
});
//prints all the 7345 Items into the console
console.log("false, all_items_from_list: ", all_items_from_list);
resolve("yay");
}
}).fail(function () {
console.log("this went horribly wrong!");
reject("ERROR!");
});
});
};
var all_items_from_list = [];
getAllItemsFromList(url).then(
function (value) { console.log("I'm here and got following data: ", all_items_from_list) },
function (error) { /* code if some error */ }
);
true: Array(5000)
false: Array(2346)
false, all_items_from_list: Array(7346)
并且缺少
async
...有时一点新鲜空气会有所帮助。await