【问题标题】:React: how using with axios 'withCredentials'反应:如何使用 axios 'withCredentials'
【发布时间】:2021-12-09 07:13:29
【问题描述】:

我有带有 API 的服务器(Node.js + Express + CORS)(作为通过 JWT 的“登录”)和通过服务器的 API 登录的 React 客户端应用程序。需要通过axios发送coockies

withCredentials = true ,

但如果服务器 + react 在 192.168.0.1 和客户端(Web 浏览器)上运行,从 192.168.0.!1 看到 - 这是下一个错误:

'Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at `http://localhost:5002/api/login. (Reason: CORS header ‘Access-Control-Allow-Origin’ does not match ‘http://localhost:3000’).`

注意:没有 withCredentials = true 的 axios 可以很好地与带有 origin: '*' 的服务器的 cors 配合使用,但在这种情况下没有来自客户端的“auto coockies”。

我的问题:使用自动 cookie 创建 React axios 的正确方法是什么 (通过

withCredentials = true

) 对于 Internet 中的任何客户端?

The error

【问题讨论】:

标签: node.js reactjs axios


【解决方案1】:

我找到了与你的问题类似的答案:enter link description here

代码运行正常,尽管“origin”必须是“*”值(字符串)而不是布尔值。

const corsConfig = {
  origin: true,
  credentials: true,
};

app.use(cors(corsConfig));
app.options('*', cors(corsConfig));

'origin' opt 应该是这样的(按文档):

const corsConfig = {
      origin: '*',
      credentials: true,
    };

Express 或 CORS 坏了 - 我不知道是哪一个。

【讨论】:

  • 我找到了我的问题的答案,而不是“类似于你的......”
猜你喜欢
  • 2021-06-15
  • 2019-01-04
  • 2022-01-16
  • 1970-01-01
  • 2020-06-28
  • 2019-01-24
  • 2020-08-19
  • 1970-01-01
  • 2021-11-11
相关资源
最近更新 更多