首页 > 解决方案 > 如何在 Sharepoint Online 中启用 CORS

问题描述

我正在尝试在我的反应应用程序中实现共享点文件浏览器。使用microsoft/file-browser这个ans代码如下。但是得到xxx... from origin 'http://localhost:3000' has been blocked by CORS policy

任何想法如何为此启用CORS

import * as React from "react";

import { GraphFileBrowser } from '@microsoft/file-browser';

class App extends React.Component {


  getAuthenticationToken() {
    return new Promise<string>(resolve => {
      resolve(
        "VALID TOKEN"
      );
    });
  }

  render() {
    return (
      <GraphFileBrowser
        getAuthenticationToken={this.getAuthenticationToken}
        onSuccess={(selectedKeys: any[]) => console.log(selectedKeys)}
        onCancel={(err: Error) => console.log(err.message)}
        endpoint='https://XXX.sharepoint.com'

      />
    );
  }
}

export default App;

标签: javascriptreactjssharepointcorsmicrosoft-graph-files

解决方案


据我所知,CORS 错误不是因为 SharePoint 而发生,而是因为您的浏览器。这里的问题是您正在尝试将资源从 xxx.sharepoint.com 加载到 localhost(默认情况下从未添加为 CORS 中接受的内容)

因此,一种解决方案是使用对 CORS 不敏感的浏览器。这可能是以下内容:

cd "C:\Program Files (x86)\Google\Chrome\Application"
chrome.exe --disable-web-security --user-data-dir="C:\tmp"

此代码段使用用户数据目录“C:\tmp”启动没有“网络安全”(包括 CORS)的 Google Chrome。

请记住,此解决方法仅适用于开发。在生产中,您不应该使用 localhost...

https://www.thepolyglotdeveloper.com/2014/08/bypass-cors-errors-testing-apis-locally/


推荐阅读