【发布时间】:2019-09-08 19:40:38
【问题描述】:
嘿,伙计们,我正在制作一个 MERN 堆栈项目,所以我面临的问题是,在来自 react 服务器的操作创建者中,我必须向节点 js 服务器发出 get 请求,或者简而言之
来自http://localhost:3000/我必须向http://localhost:5000/api/currentuser中的数据提出请求
nodejs服务器中存储的数据是{"_id":"5d73fd9003b5302f6b013a46","googleid":"109463598810933991924","__v":0}
这是从 mongodb 中获取的
所以我所做的是在我的动作创建器文件中
我喜欢
import axios from "axios";
export const fetchuser = () => {
return async(dispatch)=>{
const res=await axios.get("http://localhost:5000/api/currentuser")
dispatch({
type:'fetchuser',
payload:res.data
})
}
};
但后来在我的控制台中我遇到了错误,它说
Access to XMLHttpRequest at 'http://localhost:5000/api/currentuser' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
【问题讨论】:
-
您遇到了 CORS 问题。您是否在 Express 服务器中设置了 CORS? expressjs.com/en/resources/middleware/cors.html