为什么“Access-Control-Allow-Origin”标头说它不相等什么时候?

问题描述

我在 Heroku 上部署了我的应用程序 MERN,但我发现它根本无法与服务器交互。控制台吐出错误: “从源 'https://pacific-everglades-12315.herokuapp.com' 访问 XMLHttpRequest at 'http://localhost:8000/api/login' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:'Access-Control-Allow-Origin' 标头的值 'https://pacific-everglades-12315.herokuapp.com/' 不等于提供的来源。” 当它尝试提出请求时。我需要保留登录功能的凭据。这是我的 server.js 文件中的代码

const express = require('express');
const cors = require('cors');
const cookies = require("cookie-parser");
const port = 8000;
const app = express();
app.use(cors({
    credentials: true,origin: 'https://pacific-everglades-12315.herokuapp.com'
}));


app.use(express.json());
app.use(cookies());
require('./server/config/mongoose');

require('./server/routes/routes')(app);




const root = require('path').join(__dirname,'client','build')
app.use(express.static(root));
app.get("*",(req,res) => {
    res.sendFile('index.html',{ root });
})

app.listen(process.env.PORT || port,() => {
    console.log("Listening at Port",port)
})

React 应用加载并挂载,但它无法与服务器交互。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)