我试图从SharePoint网站的所有子网站中获取所有任务项,并在尝试将所有响应放入一个全局数组以进行进一步处理时陷入困境。 数组被填充,但是当您尝试获取元素时,它显示为未定义。
var TaskList = [];
$(document).ready(function() {
Main();
});
function Main (){
var SiteUrl = _spPageContextInfo.webAbsoluteUrl;
var main = $("#main");
function GetListItems (TragetUrl){
$.ajax({
url: SiteUrl + "//" + TragetUrl + "/_api/web/lists/getbytitle('Tasker')/items?$select=Title,Status,StartDate,Status,DueDate",
type: "GET",
data: JSON.stringify({ '__metadata': { 'type': 'SP.Data.TestListItem' }}),
headers: {
"accept": "application/json;odata=verbose",
"content-type":"application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(d) {
// TaskList.push(d.d.results)
$.each(d.d.results, function(idx, Task){
console.log(Task['Title']);
TaskList.push(Task);
});
// return TaskList;
},
error: function() {console.log('failed to get');}
});
}
function GetSubsites (BaseUrl){
$.ajax({
url: BaseUrl + "/_api/web/webs?$select=Title",
type: "GET",
data: JSON.stringify({ '__metadata': { 'type': 'SP.WebCollection' }}),
headers: {
"accept": "application/json;odata=verbose",
"content-type":"application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(d) {
console.table(d.d.results);
//process Subsites
$.each(d.d.results, function(idx, subsite){
console.log(subsite["Title"]);
GetListItems(subsite["Title"]);
});
},
error: function() {console.log('fail');}
});
}
GetSubsites(SiteUrl);
console.log(TaskList); //array output apears normal
console.log(TaskList[0]); //undefined
console.log(TaskList.length); // length is 0
}
谢谢。