我正在尝试检索SharePoint网站集中所有网站的列表,代码可以正常工作,但是我在为嵌套对象的dot.notation语法苦苦挣扎。我基本上想输出从“ .then”回调返回的对象。它可能没有用,但对我理解嵌套对象只是一个进步。脚本的最后一行是我尝试检索的站点列表。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
(function(){
var bukoName = "BUKO";// root namespace name
window.bukoName = window.bukoName || {}; //namespace alias
bk = window.bukoName
bk.SP = bk.SP || {};
bk.SP.REST = {
webs: {getWebs: function(){
var trimDuplicates = false,
queryText = 'contentclass:"STS_Web" SPSiteUrl:' + _spPageContextInfo.siteAbsoluteUrl, // get webs for the site collection
sites;
var queryUrl = window.location.protocol + "//" + window.location.hostname + "/_api/search/query?querytext='" +
queryText + "'&rowlimit=500&trimduplicates=" + trimDuplicates.toString() +
"&selectproperties='Path,Title'"; // reduce the amount of data returned to required fields
//console.log("Search query: " + queryText);
//console.log("Search query: " + queryUrl);
return jQuery.ajax({
url: queryUrl,
async: true,
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
}
})
.then(
function name(data) {
var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results,
sites = jQuery.map(results, function (result, resultIndex){
var web = {}
jQuery.each(result.Cells.results, function(propertyIndex, property){
web[property.Key] =property.Value
}); // map array dictionary to a simple object
return web;
console.log(web)
});
return sites;
console.log(sites)
}
)
}},
sites: {},
Lists: {}
}
})();
console.log(bk.SP.REST.webs.getWebs()
但是当我尝试输出getWebs函数的结果时,我得到了一个Promise对象,但是我看不到返回的实际数据。