python network_error ai_generated true

Access to fetch at 'http://localhost:5000/api' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

ID: python/flask-cors-preflight-failed

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-04-19First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active — — —
3.x active — — —

Root Cause

Flask-CORS not installed/registered, or OPTIONS preflight not handled because route restricts methods.

generic

中文

未安装/注册 Flask-CORS,或由于路由限制了方法导致 OPTIONS 预检请求未被处理。

Workarounds

  1. 95% success
    from flask_cors import CORS; CORS(app, resources={r'/api/*': {'origins': 'http://localhost:3000'}})
  2. 85% success
    Ensure route methods include OPTIONS: @app.route('/api', methods=['GET','POST','OPTIONS'])

Dead Ends

Common approaches that don't work:

  1. 65% fail

    Preflight OPTIONS still returns 405 before after_request runs.

  2. 90% fail

    CORS is server-controlled; client cannot bypass it.