# 对 /api/items 的 OPTIONS 预检请求返回 405 方法不允许

- **ID:** `python/fastapi-405-method-not-allowed-cors`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

CORSMiddleware 在路由之后添加，或 allow_methods 未包含 OPTIONS；或路由仅允许 POST。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 0.100+ | active | — | — |
| 0.110+ | active | — | — |

## 解决方案

1. **** (93% 成功率)
   ```
   app.add_middleware(CORSMiddleware, allow_origins=['http://localhost:3000'], allow_methods=['*'], allow_headers=['*'])  # add before routes
   ```
2. **** (85% 成功率)
   ```
   Ensure the route decorator uses @app.post('/api/items') and CORSMiddleware is registered at startup.
   ```

## 无效尝试

- **** — Fails when CORSMiddleware is not registered first. (65% 失败率)
- **** — Default allow_methods is ['GET']; OPTIONS still blocked. (70% 失败率)
