python
network_error
ai_generated
true
访问 'http://localhost:5000/api' 被 CORS 策略阻止:请求的资源上没有 'Access-Control-Allow-Origin' 头。
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
80%修复率
86%置信度
0证据数
2024-04-19首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 2.x | active | — | — | — |
| 3.x | active | — | — | — |
根因分析
未安装/注册 Flask-CORS,或由于路由限制了方法导致 OPTIONS 预检请求未被处理。
English
Flask-CORS not installed/registered, or OPTIONS preflight not handled because route restricts methods.
解决方案
-
95% 成功率
from flask_cors import CORS; CORS(app, resources={r'/api/*': {'origins': 'http://localhost:3000'}}) -
85% 成功率
Ensure route methods include OPTIONS: @app.route('/api', methods=['GET','POST','OPTIONS'])
无效尝试
常见但无效的做法:
-
65% 失败
Preflight OPTIONS still returns 405 before after_request runs.
-
90% 失败
CORS is server-controlled; client cannot bypass it.