gpt4 book ai didi

SharePoint 中的 JavaScript REST GET 变量范围困惑

转载 作者:行者123 更新时间:2023-11-28 06:04:31 24 4
gpt4 key购买 nike

使用下面的代码,我尝试获取 SharePoint 列表中满足特定条件的记录/行数。然而,我在变量范围方面遇到了麻烦。如何让 getListData() 返回 numRecords 以便我可以将其设置为新变量。使用以下语法,numRecords 是 onQuerySucceeded 函数中的正确值(通过 console.log 验证),但在顶级函数中未定义。

谢谢!

<!DOCTYPE html>
<head>
<script>

$(function () {
var numQuals = 0;

numQuals = getListData(1);
alert(numQuals);
});

function getListData(myFilter) {

$.ajax({
url: "https://mysite.sharepoint.com/tools/_api/web/lists/GetByTitle('C&Q Documents')/items?$select=Document_x0020_TypeId&$filter=Document_x0020_TypeId eq " + myFilter,
type: "GET",
headers: {"accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose"},
success: onQuerySucceeded,
error: onQueryFailed
});

}

function onQuerySucceeded(data) {
var numRecords = 0;

$.each(data.d.results, function (key, value) {
numRecords++;
});

console.log("in onQuerySucceeded: " + numRecords);
return numRecords
}

function onQueryFailed(sender, args) {console.log("query failed!");}

</script>
</head>
<body></body>
</html>

最佳答案

想通了。只需要进行调用同步,即:

$.ajax({
async: false,
url: "https://mysite.sharepoint.com/tools/_api/web/lists/GetByTitle('C&Q Documents')/items?$select=Document_x0020_TypeId&$filter=Document_x0020_TypeId eq " + myFilter,
type: "GET",
headers: {"accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose"},
success: onQuerySucceeded,
error: onQueryFailed
});

感谢所有贡献者!

关于SharePoint 中的 JavaScript REST GET 变量范围困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37008313/

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