python network_error ai_generated true

CORS error: Origin http://example.com is not allowed by Access-Control-Allow-Origin

ID: python/flask-cors-error-origin

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-01-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

The CORS policy on the server does not allow the requesting origin.

generic

中文

服务器上的CORS策略不允许请求的来源。

Workarounds

  1. 95% success Use flask-cors to enable CORS for specific origins.
    from flask_cors import CORS
    CORS(app, origins=['http://example.com'])
  2. 85% success Set CORS headers manually in a response.
    @app.after_request
    def add_cors_headers(response):
        response.headers['Access-Control-Allow-Origin'] = 'http://example.com'
        return response

Dead Ends

Common approaches that don't work:

  1. Adding a wildcard '*' to allowed origins but not handling credentials. 70% fail

    Wildcard origins cannot be used with credentials in some cases.

  2. Ignoring the error and assuming it will work on production. 90% fail

    The error will persist in production if not fixed.