【问题标题】:Rest 403 Forbidden SharePoint Framework ApplicationRest 403 禁止的 SharePoint 框架应用程序
【发布时间】:2018-01-13 21:15:32
【问题描述】:

我正在尝试学习 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));
};

【问题讨论】:

    标签: reactjs rest sharepoint sharepointframework


    【解决方案1】:

    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);  
            });  
          });  
    

    【讨论】:

    • 我将代码更新为您在此处拥有的代码,并且我收到 sp-webpart-workbench-assembly_en-us_6c0b8d4939075de76b90faa8480cb359.js:206 Uncaught TypeError: Cannot read property 'get' of undefined。当我添加 console.log(this.context);我不确定我不确定托管环境的问题出在哪里。我创建了一个全新的项目并使用新项目进行测试,结果相同。问题可能出在我的网站集上吗?
    • 请看实现here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 2020-10-17
    • 2021-09-25
    • 1970-01-01
    • 2017-11-09
    • 2019-10-29
    • 1970-01-01
    相关资源
    最近更新 更多