0

我正在尝试学习反应并正在创建一个 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));
};
4

1 回答 1

0

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);  
        });  
      });  
于 2018-01-14T13:24:33.950 回答