# CORS错误：来源 http://example.com 未被 Access-Control-Allow-Origin 允许

- **ID:** `python/flask-cors-error-origin`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

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

## 无效尝试

- **Adding a wildcard '*' to allowed origins but not handling credentials.** — Wildcard origins cannot be used with credentials in some cases. (70% 失败率)
- **Ignoring the error and assuming it will work on production.** — The error will persist in production if not fixed. (90% 失败率)
