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
80%Fix Rate
85%Confidence
0Evidence
2025-01-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
The CORS policy on the server does not allow the requesting origin.
generic中文
服务器上的CORS策略不允许请求的来源。
Workarounds
-
95% success Use flask-cors to enable CORS for specific origins.
from flask_cors import CORS CORS(app, origins=['http://example.com'])
-
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:
-
Adding a wildcard '*' to allowed origins but not handling credentials.
70% fail
Wildcard origins cannot be used with credentials in some cases.
-
Ignoring the error and assuming it will work on production.
90% fail
The error will persist in production if not fixed.