【发布时间】:2021-07-06 12:35:26
【问题描述】:
我尝试将本地后端与前端连接
axios({
method: "post",
url: "http://localhost:5000/api/user/login",
withCredentials: true,
data: {
email,
password,
},
})
.then((res) => {
if (res.data.errors) {
emailError.innerHTML = res.data.errors.email;
passwordError.innerHTML = res.data.errors.password;
} else {
window.location = "/";
}
})
.catch((err) => {
console.log(err);
});
};
和 Reactjs。后端与邮递员配合得很好,但是当我尝试从前端登录时,在 console.log 中我收到错误消息“cors 问题”... 我错过了什么? 这是错误信息: 从源“http://localhost:3000”访问“http://localhost:5000/api/user/login”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:当请求的凭据模式为“包含”时,响应中“Access-Control-Allow-Origin”标头的值不能是通配符“*”。 XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。
这是提交后运行的函数的代码
【问题讨论】:
-
您必须在后端 cors 配置中允许 localhost。如果您使用的是快递,请尝试查看:stackoverflow.com/questions/43150051/…
标签: node.js reactjs mongodb express