【问题标题】:Python flask cors failing for POST but passing for GETPython烧瓶cors未能通过POST但通过GET
【发布时间】:2021-07-18 21:03:33
【问题描述】:

我使用 python flask 创建了一个后端 api,并托管在 gcp 计算引擎上。

当我尝试使用网站 www.example.com 访问 api 并在 java 脚本中调用 fetch() 时,出现 CORS 错误

Access to fetch at 'https://{ip}/api' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

当我拨打GET 时,电话正常,但我的POST 电话不正常,并出现上述错误

下面是失败的 POST 调用

@app.route('/api/v1/xxx', methods=['POST'])
@cross_origin(origin='localhost',headers=['Content- Type'])
def upload_file():
    #some code
        return "successfully"

下面是工作中的get调用

@app.route('/api/v1/xxx', methods=['GET'])
@cross_origin(origin='localhost',headers=['Content- Type'])
def signUpCheck():
    #some code
    return jsonify(fun())

【问题讨论】:

  • 在该 python/flask 环境的日志中的服务器端,服务器在发送对该 POST 请求的响应之前记录了哪些消息?

标签: javascript python nginx flask cors


【解决方案1】:

我找到了解决方案的答案, 所以发生的事情是我通过 nginx 路由所有呼叫,因此在服务器端我只收到选项呼叫,没有别的。 当我尝试通过邮递员而不是 javascript 调用 API 时,我收到了文件大小异常。我必须在我的 nginx 文件中添加以下行来解决这个问题

client_max_body_size 100M;

一般来说,如果服务器端出现问题,您会收到 cors 错误,所以也请尝试通过邮递员调用您的 api

【讨论】:

    猜你喜欢
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2018-04-29
    • 1970-01-01
    • 2018-06-15
    • 2020-09-10
    • 1970-01-01
    相关资源
    最近更新 更多