Json SharePoint列表上的REST GET方法
早上好 我是REST新手,我想做的是证明我可以在SharePoint列表中获得一个已排序的列(Exchangex0020),但我不确定我是否以正确的API格式放置了正确的信息,我在Visual Studio代码中得到了很多扭曲的红色!代码如下:Json SharePoint列表上的REST GET方法,json,rest,sharepoint,api-design,Json,Rest,Sharepoint,Api Design,早上好 我是REST新手,我想做的是证明我可以在SharePoint列表中获得一个已排序的列(Exchangex0020),但我不确定我是否以正确的API格式放置了正确的信息,我在Visual Studio代码中得到了很多扭曲的红色!代码如下: function getListItemById(/sites/it/Tools/IT-Contracts/Lists/IT% 20Contracts/AllItems.aspx, Currency Exchange Rates, Exchange_
function getListItemById(/sites/it/Tools/IT-Contracts/Lists/IT%
20Contracts/AllItems.aspx, Currency Exchange Rates, Exchange_x0020,
success, failure) {
var url = webUrl + "/_vti_bin/listdata.svc/" + "Currency Exchange Rate + "(" +Exchange_x0020 + ")";
$.ajax({
url: url,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
success(data.d);
},
error: function (data) {
failure(data.responseJSON.error);
}
});
}
//Usage
getListItemById(/sites/it/Tools/IT-Contracts/Lists/CurrencyExchangeRates/AllItems.aspx,'Tasks',2,function(taskItem){
console.log(taskItem.TaskName);
},
function(error){
console.log(JSON.stringify(error));
}
一般JavaScript语法问题 在函数定义中,需要更加小心地使用参数名 您现在拥有的:
function getListItemById(/sites/it/Tools/IT-Contracts/Lists/IT%20Contracts/AllItems.aspx,
Currency Exchange Rates, Exchange_x0020, success, failure)
每个参数都应该对命名变量有效。斜杠、空格、减号和百分比符号在变量名中无效
更有意义的是:
function getListItemById(webUrl, listName, columnName, success, failure)
然后需要更新函数的第一行以使用新的参数名称:
var url = webUrl + "/_vti_bin/listdata.svc/" + listName + "(" + columnName + ")";
在函数体中,还应注意变量名周围的引号。listName
变量前面不应有引号(您的代码当前显示的是+“货币汇率+
)
使用SharePoint REST Web服务
要从列表中检索项目并按特定列排序,请使用$orderby
参数
"/_vti_bin/ListData.svc/CurrencyExchangeRates?$orderby=ExchangeRate"
请注意,2010 REST web服务使用列表标题和列显示名称,并删除空格,如上例所示。2013 REST web服务允许您使用实际的列表标题或列表的GUID
"/_api/web/lists/getbytitle('" + listName + "')/items?$orderby=ExchangeRate"
有关更多详细信息,请参阅。谢谢!对!请原谅我的愚蠢,但在函数getListItem中,我实际上不需要将URL、listName等放在我现在得到的下面,它不会在控制台中返回成功或失败,但我想我离…函数getListItemById(webUrl、listName、itemID、success、failure)更近了{var url=webUrl+“/”vti\u bin/ListData.svc/CurrencyExchangeRates?$orderby=ExchangeRate“(“+Exchange\u x0020+”);$.ajax({url:url,//+”/sites/it/Tools/it Contracts/list/it%20Contracts/AllItems.aspx(“+货币汇率+”)/items(“+Exchange\u x0020+”),方法:“获取”,标题:{“接受”:application/json;odata=verbose“},@Dazza测试您的URL是否正确构建的一个好方法是尝试直接在浏览器中导航到它。如果URL有效,Internet Explorer将其显示为提要。如果无效,您将收到400个错误请求(找不到页面)。
[网站URL]/_vti_bin/ListData.svc/ListTitle?$orderby=ColumnDisplayName
用列表的标题(删除空格)替换ListTitle
,用列的显示名称(删除空格)替换ColumnDisplayName
。URL中不需要任何括号。