我已经检查了该网站和其他网站,并找到了我尝试过的答案,但肯定缺少正确的答案。第一次涉足CSOM是因为我通常在C#服务器端进行此类操作。如您在getScript函数中所看到的,我已经撤消了我能想到的所有内容,但仍然不好。文档声称已准备就绪,我已通过写入console.log进行确认。我想念的是什么。错误在函数retrieveListItems()的第二行。
<html>
<head>
<title></title>
<script src="scripts/jquery-1.7.2.min.js" type="text/javascript">
</script>
<script type="text/javascript" src="scripts/MicrosoftAjax.js">
</script>
</head>
<body>
<script type="text/javascript">
var hostweburl;
// Load the required SharePoint libraries.
// Get the URI decoded URLs.
hostweburl = "https://hubble.whanganui.govt.nz";
var scriptbase = hostweburl + "/_layouts/15/";
// Load the js files and continue to
// the execOperation function.
$.getScript(scriptbase + "init.js");
$.getScript(scriptbase + "MicrosoftAjax.js");
$.getScript(scriptbase + "sp.core.js");
$.getScript(scriptbase + "sp.runtime.js");
$.getScript(scriptbase + "sp.js");
$(document).ready(function(){
//ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
console.log("ready to roll");
retrieveListItems("https://path_to_my_SP_site")
});
// Continue your program flow here.
function retrieveListItems(siteUrl){
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Announcements');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
'<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
'<Value Type=\'Number\'>1</Value></Geq></Where></Query>' +
'<RowLimit>10</RowLimit></View>'
);
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo += '\nID: ' + oListItem.get_id() +
'\nTitle: ' + oListItem.get_item('Title') +
'\nBody: ' + oListItem.get_item('Body');
}
alert(listItemInfo.toString());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
</script>
</body>
</html>