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

Docker Compose同网络下FastAPI与React间CORS致OPTIONS请求失败

问题排查:Docker Compose默认网络中React向FastAPI发送POST请求时CORS预飞行失败

在Docker Compose默认网络环境下,React前端向FastAPI后端的/calculate路由发送POST请求时,预飞行OPTIONS请求因CORS规则失败。但本地通过Postman请求FastAPI完全正常,进入React容器执行curl -X OPTIONS请求也无异常。已为FastAPI配置了允许所有源、方法、请求头的CORS中间件参数,却始终无法解决问题,附上相关代码请帮忙排查原因。

FastAPI 代码

# Fast API setup
app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.post("/calculate")
def read_root(calculation: PrimeCalculation):
    return {"Ok": "True"}

React 代码

const response = await fetch(calculate_url + calculate_endpoint, {
  method: "POST",
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(request)
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 19:04:55