gpt4 book ai didi

javascript - Sharepoint Javascript 页面加载失败

转载 作者:行者123 更新时间:2023-12-03 02:50:03 24 4
gpt4 key购买 nike

我正在尝试获取要放入 select 元素的 option 中的学校列表。我希望在页面加载时添加下拉选项,但我没有做正确的事情。如果直接使用 button 元素调用下面的代码,则它可以工作,但在页面加载时不起作用。

我收到的错误是“对象不支持此操作”,行 var clientContext = new SP.ClientContext(siteUrl);

var siteUrl= '/learning/schools';

window.load = init();

function init(){

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems());
}

function retrieveListItems() {

var clientContext = new SP.ClientContext(siteUrl);

var oList = clientContext.get_web().get_lists().getByTitle('Schools');

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();

var schoolCodes = document.getElementById('classSchoolList');

while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var schoolOption = document.createElement('option');
schoolOption.value = oListItem.get_item('Title');
schoolOption.text = oListItem.get_item('Title') + " : " + oListItem.get_item('School');
schoolCodes.add(schoolOption);
}


}

function onQueryFailed(sender, args) {

alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

}

最佳答案

你能尝试一下吗

//load the sp.js script and execute the init method
//no guarantee sp.js will be fully loaded before init() runs.
SP.SOD.executeFunc('sp.js', init());

function init(){
//ensure script is fully loaded, then execute retrieveListItems()
ExecuteOrDelayUntilScriptLoaded(retrieveListItems(), 'sp.js');
}

或者,

SP.SOD.executeFunc('sp.js', null, function(){
ExecuteOrDelayUntilScriptLoaded(retrieveListItems(), 'sp.js');
});

ExecuteOrDelayUntilScriptLoaded 将等待“sp.js”文件完成加载,然后再调用 retrieveListItems()。但如果'sp.js'没有被请求,它将永远等待。这就是为什么我们首先调用 executeFunc 来请求“sp.js”。然而,它并不能保证在继续之前它已加载。它只是将其添加到需要加载的脚本堆栈中,然后运行回调。

关于javascript - Sharepoint Javascript 页面加载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47915271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com