javascript - 如何在 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;
解决方案
据我所知,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/
推荐阅读
- qt - Qt 5.11 无法通过 QSettings 将配置值保存到 .ini 文件
- javascript - 在 Redux 中将一个 reducer 结果传播到 combineReducers
- python - 重新组织列表
- php - 使用 PHP 更改值将 CSV 插入 MySQL
- java - 从命令行调用 Java - Linux 与 Windows
- android - 后台服务上的应用程序监控位置
- javascript - 收到错误:未捕获(承诺)服务器响应无效?
- android - ConstraintLayout 中的 MPAndroid 图表以及其他小部件(它们不可见)
- pandas - 从 Pandas 中的 DataFrame 中提取某些行的最短方法
- angular - 在 ag-grid serverSide rowModelType 上实现过滤器