无法让 ClientContext.executeQuery() 在 javascript 中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12147472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't get ClientContext.executeQuery() to work in javascript
提问by Josh
I am getting information from a sharepoint list and then I want to use that data. The problem is that I need the data to be updated from the Sharepoint server before I use it, but I can't get executeQuery() to work. I can get executeQueryAsync() to work though. Here is my code:
我从共享点列表中获取信息,然后我想使用该数据。问题是我需要在使用 Sharepoint 服务器之前更新数据,但我无法让 executeQuery() 工作。我可以让 executeQueryAsync() 工作。这是我的代码:
// Global variables;
var context;
var web;
var list;
var howManyItem = 0;
var allItems;
var randNums = [];
// Initializes the variables; Sets listname; Gets all items;
function init(){
context = new SP.ClientContext.get_current();
web = context.get_web();
// Enter the list name;
this.list = web.get_lists().getByTitle('LetsTalkAdded');
// Get item count in the query/list;
var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
context.load(allItems, 'Include(Title)');
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
This works fine, but when I change the last line to:
这工作正常,但是当我将最后一行更改为:
context.executeQuery(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
It no longer works, but I can't run them asyncronously. If I do, the parts of my code that depend on that information don't work. Why doesn't the executeQuery() funcion work?
它不再有效,但我无法异步运行它们。如果我这样做了,我的代码中依赖于该信息的部分将不起作用。为什么 executeQuery() 函数不起作用?
回答by user2610826
I have been trying something similar in using ExecuteQuery instead of the Async version. And from what I have found the non Async version is not supported in the JSOM. So you have to use the Async version.
我一直在尝试使用 ExecuteQuery 而不是 Async 版本的类似方法。从我发现的 JSOM 中不支持非 Async 版本。所以你必须使用 Async 版本。
Looking at his number 6. http://blogs.msdn.com/b/sharepointdev/archive/2011/07/19/working-with-the-ecmascript-client-object-model-jsom-in-sharepoint-2010-part-3-nikhil-sachdeva.aspx
回答by Clem
executeQueryAsync cannot populate global variables. It's only use is to display an alert to the user. Other than that there is no use for it.
executeQueryAsync 无法填充全局变量。它的唯一用途是向用户显示警报。除此之外没有任何用处。