reactjs - 使用 fetch 进行 api 调用时无法授权
问题描述
反应代码以获取 api 数据
getData = () => {
const url = FormatUrl(`/cluster`)
fetch(url, {
method: 'GET',
headers: new Headers({
'Authorization': 'Basic my_token',
'Content-Type': 'application/x-www-form-urlencoded'
})})
.then(res => res.json())
.then(res => {
console.log(res, "=")
})
}
错误:
Access to fetch at 'https://beta.cnox.io:3001/cluster' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.
beta.cnox.io:3001/cluster:1 Failed to load resource: net::ERR_FAILED
在这里,我正在尝试从上面的 api 获取数据并发送令牌,但得到以下错误。相同的 api 是从角度工作的。请看一下
解决方案
推荐阅读
- git - 使用 git revert 时如何解决此冲突?
- activemq - 带有 JMS 2.0 的 ActiveMQ 5.15
- java - 使用 PowerMockito 模拟对象创建
- c# - 无法将类型“System.Collections.Generic.List<>”转换为“System.Collections.Generic.IList<>”c#MongoDB
- flutter - 在 Flutter 中的屏幕之间来回导航时,保持 TextFormFields 中的用户输入持续存在
- ios - 如何快速找到哪个应用程序中断了音频会话
- python - 在 React 后端添加变量
- javascript - 如何自动打开“折叠”菜单?
- ibm-cloud - 如何使用 softlayer api 从快照创建块存储
- python - 如何在熊猫数据框中使过滤器可配置?