0

我想使用JavaScript檢索列表數據。但有些事情出錯了。我試圖調試代碼,但我無法理解那件事。
以下是JavaScript代碼:如何檢索使用JQuery或JavaScript和填充DropDownList Sharepoint 2013列表數據?

ExecuteOrDelayUntilScriptLoaded(PopulateDepartments, "sp.js"); 
var _ctx = null; 
var _web = null; 
var _allItems = null; 

function PopulateDepartments() { 
    debugger; 
    _ctx = SP.ClientContext.get_current(); 
    _web = _ctx.get_web(); 
    var list = _web.get_lists().getByTitle("ServiceType"); 
    var query = new SP.CamlQuery(); 
    query.set_viewXml("<View><Query><OrderBy><FieldRef Name='Title'/></OrderBy></Query></View>"); 
    _allItems = list.getItems(query); 
    _ctx.load(_allItems, 'Include(Title,ID)'); 
    debugger; 
    _ctx.executeQueryAsync(Function.createDelegate(this, this.PopulateDepartmentSuccess), 
     Function.createDelegate(this, this.PopulateDepartmentFaild)); 
} 

function PopulateDepartmentSuccess() { 
    var ddlEntry = this.document.getElementById("ddl1"); 
    ddlEntry.options.length = 0; 
    var listEnumerator = _allItems.getEnumerator(); 
    while (listEnumerator.moveNext()) { 
     var currentItem = listEnumerator.get_current(); 
     ddlEntry.options[ddlEntry.options.length] = new Option(currentItem.get_item("Title"), currentItem.get_item("ID")); 
    } 
} 

function PopulateDepartmentFaild() { 
    alert("Something went Wrong....!!"); 
} 

每當我運行此代碼它表明我警告框。
請幫助..

+0

你試過在你的查詢中只使用''並將它設置爲查詢屬性而不是'set_viewXml'? – MatRt 2013-02-15 05:04:09

+0

嘿@ user1073122我試過那個,但它不工作。它仍然顯示警報彈出窗口。 – 2013-02-15 05:24:49

+0

我認爲你修復了PopulateDepartmentFaild拼寫錯誤? – 2013-02-16 15:25:46

回答

2

有些時候,這並不需要正確的reference.Check如果它。所以除刪除此 _ctx.executeQueryAsync(Function.createDelegate的這個參考(這一點,this.PopulateDepartmentSuccess工作時間), Function.createDelegate(this,this.PopulateDepartmentFaild));

嘗試使用這樣的事情

_ctx.executeQueryAsync(PopulateDepartmentSuccess,PopulateDepartmentFaild);

+1

感謝您的回答。我通過其他方式解決了問題。 – 2014-05-13 09:21:51

0

我想。如果您創建Sharepoint應用程序,則需要在AppManifest.xml中爲Web授予權限。

+0

請詳細解釋。否則,這應該是一個評論。沒有答案。如果您沒有足夠的回購,請等待,耐心等待,積極產生回購。 – 2016-10-24 07:45:34

相關問題