python network_error ai_generated true

从源 'http://app.local' 访问 'http://api.local/users' 的 XMLHttpRequest 已被 CORS 策略阻止:请求的资源上不存在 'Access-Control-Allow-Origin' 头。

Access to XMLHttpRequest at 'http://api.local/users' from origin 'http://app.local' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

ID: python/flask-cors-no-access-control-origin

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2024-07-07首次发现

版本兼容性

版本状态引入弃用备注
4.x active — — —

根因分析

未初始化 Flask-CORS,或源不在允许列表中,导致浏览器阻止了跨域响应。

English

Flask-CORS was not initialized, or the origin was not in the allowed list, so the browser blocked the cross-origin response.

generic

解决方案

  1. 95% 成功率
    from flask_cors import CORS
    CORS(app, origins=['http://app.local'], supports_credentials=True)
  2. 80% 成功率
    @app.route('/users', methods=['GET','OPTIONS'])
    def users(): ...

无效尝试

常见但无效的做法:

  1. 85% 失败

    Preflight OPTIONS requests still fail, so POST/PUT never reach the server.

  2. 90% 失败

    Browsers reject wildcard origins when credentials are included.