简体   繁体   English

飞行前响应FLASK CORS中Access-Control-Allow-Methods不允许使用方法PUT

[英]Method PUT is not allowed by Access-Control-Allow-Methods in preflight response FLASK CORS

GETs are working right. GET工作正常。 I can't seem to make PUT work and I fear POST is not working as well. 我似乎无法使PUT正常工作,而且我担心POST也无法正常工作。

Kept on having the error 继续发生错误

Access to XMLHttpRequest at '<...>' from origin 'https://localhost:3000' has been blocked by CORS policy: Method PUT is not allowed by Access-Control-Allow-Methods in preflight response.

I have a local webapp that calls on an external api which means CORS will be one of the hurdles I should tackle. 我有一个本地Web应用程序,该应用程序需要一个外部api,这意味着CORS将是我应该解决的障碍之一。

Here's my flask code. 这是我的烧瓶代码。

@blueprint.route('/profiles/<oi_id>', methods=['POST'])
@cross_origin(send_wildcard=True, methods=['POST', 'OPTIONS'])
def create_checkin_profile(oi_id):
    return jsonify(cph.create_owner_profile_info(oi_id, json.loads(request.data)))


@blueprint.route('/profiles/<oi_id>', methods=['PUT'])
@cross_origin(send_wildcard=True, methods=['PUT', 'OPTIONS'])
def edit_checkin_profile(oi_id):
    return jsonify(cph.edit_owner_profile_info(oi_id, json.loads(request.data)))

It's quite confusing since I'm pretty sure I allowed PUT. 因为我很确定我允许PUT,所以这非常令人困惑。 I'm using axios for this on a react app. 我在React应用程序上为此使用axios。 Here's my call-api function. 这是我的call-api函数。

axios({
      method: 'PUT',
      url: url,
      timeout: 3000,
      data: data,
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Content-Type": "application/json",
        Accept: "application/json",
      },
    });

Alright I fixed this. 好吧,我解决了这个问题。 Apparently according to the docs I have to specify "Content-Type" on the cross origin requests. 显然,根据文档,我必须在跨源请求中指定“ Content-Type”。 Like so: 像这样:

@cross_origin(allow_headers=['Content-Type'])

Don't be fooled by the default of allow all. 默认为allow all,请不要上当。 You need to specify this apparently. 您显然需要指定此。

I have to remove unnecessary headers such as Accept and Access-Control-Allow-Origin. 我必须删除不必要的标头,例如Accept和Access-Control-Allow-Origin。 Updating my axios like so: 像这样更新我的axios:

axios({
    method: 'PUT',
    url: url,
    timeout: 3000,
    data: data,
    headers: {
        "Content-Type": "application/json",
    },
});

BONUS: 奖金:

I ran into a problem if my api calls returned a non 200 response with preflight request, the response body becomes null. 如果我的api调用返回了一个非200的预检请求响应,则我遇到了一个问题,响应主体变为null。 I just added these lines of code on my main application. 我只是在主应用程序中添加了这些代码行。 I needed the body for the details of the error is there. 我需要正文以获取错误的详细信息。

@app.after_request
def set_cors_header(response):
    response.headers['Access-Control-Allow-Origin'] = '*'
    return response

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 飞行前响应中的Access-Control-Allow-Headers不允许请求标头字段Access-Control-Request-Methods - Request header field Access-Control-Request-Methods is not allowed by Access-Control-Allow-Headers in preflight response “请求 header 字段 Access-Control-Allow-Origin 在预检响应中被 Access-Control-Allow-Headers 不允许”尽管 CORS 配置有效 - “Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response” despite valid CORS config CORS 策略:对预检请求的响应未通过访问控制 - CORS policy: Response to preflight request doesn't pass access control 如何允许 CORS 与 Flask 获得对预检请求的响应没有 http 正常状态 - How to allow CORS with Flask getting Response to preflight request does not have http ok status Django CORS 问题:不允许访问控制允许来源 - Django CORS issue: access-control-allow-origin is not allowed Flask CORS - 重定向上没有Access-control-allow-origin标头() - Flask CORS - no Access-control-allow-origin header present on a redirect() 被 CORS 策略阻止:No"Access-Control-Allow-Origin" Using Flask - Blocked by CORS policy: No"Access-Control-Allow-Origin" Using Flask Flask / Flask-CORS:缺少CORS标头“ Access-Control-Allow-Origin” - Flask/Flask-CORS: CORS header ‘Access-Control-Allow-Origin’ missing 瓶子 PY:响应预检请求,无“访问控制允许来源”header - Bottle PY: Response to preflight request, No 'Access-Control-Allow-Origin' header 我在React中编写axios post方法并添加Access-Control-Allow-Origin,但请求无法正常工作,并且响应我cors原点错误 - I write axios post method in React and add Access-Control-Allow-Origin but request is not working and response to me cors origin error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM