我正在尝试学习 React 并正在创建 SharePoint 框架 Web 部件应用程序。我正在尝试使用 fetch from react 从站点上的列表中检索数据。我在托管的 SharePoint 工作台中工作。
当我从一个按钮点击运行它时,我收到 403 禁止。当我在邮递员中设置相同的休息调用时,我得到了预期的数据返回。我相信我需要将 X-RequestDigest 添加到标题中,但是 document.getElementById("__REQUESTDIGEST") 返回 null。我在 SharePoint 工作台中做错了什么?
submit = e => {
fetch(
"https://MY_URL_HERE.sharepoint.com/_api/lists/getbytitle('MY_LIST_NAME_HERE')/items",
{
method: "GET",
headers: {
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"odata-version": "",
"IF-MATCH": "*",
"X-HTTP-Method": "MERGE",
}
}
).then(response => console.log(response));
};
SPFx 提供了一个内置httpclient
来进行 REST API 调用。
不,如果您在 SharePoint 托管工作台中,则要获取列表项,您不需要请求摘要。
修改您的代码,如下所述:
添加以下导入语句:
import { SPHttpClient, SPHttpClientResponse, SPHttpClientConfiguration }
from '@microsoft/sp-http';
现在,您可以使用以下代码代替 fetch:
this.context.spHttpClient.get("https://MY_URL_HERE.sharepoint.com/_api/lists/getbytitle('MY_LIST_NAME_HERE')/items",
SPHttpClient.configurations.v1)
.then((response: SPHttpClientResponse) => {
response.json().then((responseJSON: any) => {
console.log(responseJSON);
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句