You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask+React跨域CORS问题求助:Windows正常Mac报错

Flask + React跨域CORS异常:Windows正常运行,Mac触发预检请求报错

报错信息

Access to fetch at 'http://localhost:5000/login' from origin 'http://localhost:3000' has been blocked by CORS policy
完整错误:预检请求未通过访问控制检查,请求资源缺少'Access-Control-Allow-Origin'头

环境配置

  • 后端:Python-Flask,运行在 localhost:5000
  • 前端:React,运行在 localhost:3000

已配置内容

Flask CORS配置

CORS(app, supports_credentials=True, resources={r"/*": {"origins": "*"}})

前端Fetch请求代码

try{
     const response = await fetch('http://localhost:5000/login', {
          method: 'POST',
          headers: {
               'Content-Type': 'application/json',
               'Access-Control-Allow-Origin': 'http://localhost:3000',
               'Access-Control-Allow-Credentials': 'true'
          },
          body: JSON.stringify(dataToSend),
          credentials: 'include'            
     })
     const data = await response.json();
     console.log("JSON Message: " + data.message);
     setPostData(data.message);
     setPostError(data.error);
            
} catch (error) {
     console.log(error);
     setPostError(error);
}

问题现状

  1. 代码在Windows环境下完全正常,但在Mac上触发上述CORS报错
  2. 已尝试Stack Overflow相关解决方案、清除Chrome浏览器缓存,问题未解决
  3. 使用curl发送OPTIONS请求到http://localhost:5000/login,返回的响应头完全符合预期:
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Allow: HEAD, GET, POST, OPTIONS
Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authorization, Content-Type
Access-Control-Allow-Methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT
Vary: Origin
Content-Length: 0
Server: Werkzeug/2.0.3 Python/3.9.13
Date: Sat, 30 Dec 2023 16:56:39 GMT

求各位提供可行的解决建议,感谢!

内容的提问来源于stack exchange,提问作者Josh S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 03:41:15