我在 Express 服务器上有一个 REST api,在前端有一个 React 应用程序。我设计它是为了在遇到错误时将 JSON 发送到前端,它会发送它,我可以使用它在客户端作为模式等打印错误。这是我的用户/登录的路由文件(我也使用 JWT和 bcrypt 解决密码问题):
router.post("/login", (req, res) => {
const { email, password } = req.body;
//simple validation
if (!email || !password) {
return res.status(400).json({ general: "Email and Password can not be empty" });
}
//check for existing user
User.findOne({ email }).then((err, user) => {
if (!user)
return res.status(400).json({ email: "This user doesn't exist"});
if (err) console.log(err);
//Validate password
bcrypt.compare(password, user.password).then(isMatch => {
if (!isMatch)
return res
.status(400)
.json({ password: "Password and …Run Code Online (Sandbox Code Playgroud) 我在后端有一个 React 应用程序和 Nodejs(Express)。部署到主机服务器后,我为更新某些文档所做的功能停止正常工作。它给出了 CORS 错误:
我有这行代码来处理 server.js 中的 CORS 策略:
app.use((req, res, next) => {
res.set({"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Methods" : "HEAD, OPTIONS, GET, POST, PUT, PATCH, DELETE",
"Access-Control-Allow-Headers" : "Content-Type, Authorization, X-Requested-With"})
next();
});
Run Code Online (Sandbox Code Playgroud)
GET 和 POST 方法可以,但对 PUT 不起作用(不知道 Delete 是否有效没试过)
我在这个问题上有很多时间;试过这个:https : //stackoverflow.com/a/42463858/11896129
在网上寻找了一堆解决方案,尝试从 IIS web.config 文件配置它,但没有解决我的问题。我可能会错过哪个部分?