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
相关产品推荐
相关产品推荐

