【发布时间】:2020-08-04 11:20:29
【问题描述】:
React 应用程序位于端口 3000,而我的服务器位于端口 4000。 从我的 Redux 操作中,我正在调用这条路线
axios.get("localhost:4000/auth/user", config)
.then(res => dispatch({
type: USER_LOADED,
payload: res.data
}))
并得到这个错误:
在 'localhost:4000/auth/user' 从源访问 XMLHttpRequest 'http://localhost:3000' 已被 CORS 策略阻止:跨源 请求仅支持协议方案:http、data、chrome、 chrome 扩展,https。
即使我已经将这些 CORS 设置添加到服务器:
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With')
next()
})
我错过了什么?
【问题讨论】:
标签: javascript cors