python config_error ai_generated true

ValueError: CORS preflight request failed

ID: python/starlette-cors-middleware

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-10-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Starlette 的 CORSMiddleware 配置不正确,例如 allow_origins 未包含请求来源。

generic

中文

Starlette 的 CORSMiddleware 配置不正确,例如 allow_origins 未包含请求来源。

Workarounds

  1. 95% success
    app.add_middleware(CORSMiddleware, allow_origins=['http://localhost:3000'], allow_methods=['*'], allow_headers=['*'])
  2. 80% success
    allow_origin_regex='https?://.*'

Dead Ends

Common approaches that don't work:

  1. 90% fail

    浏览器不允许通配符与凭证同时使用。

  2. 60% fail

    预检请求会失败因为未允许 OPTIONS。