本文介绍了Flask-CORS 不适用于 POST,但适用于 GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地运行 Flask-Restful API,并从不同端口发送包含 JSON 的 POST 请求.我收到错误

I'm running a Flask-Restful API locally and sending a POST request containing JSON from a different port. I'm getting the error

No 'Access-Control-Allow-Origin' header is present on the requested resource.

但是,当我跑步时

curl --include -X OPTIONS http://localhost:5000/api/comments/3
        --header Access-Control-Request-Method:POST
        --header Access-Control-Request-Headers:Content-Type
        --header Origin:http://localhost:8080

我明白

HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Allow: HEAD, GET, POST, OPTIONS
Access-Control-Allow-Origin: http://localhost:8080
Access-Control-Allow-Methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT
Vary: Origin
Access-Control-Allow-Headers: Content-Type
Content-Length: 0

将Access-Control-Allow-Origin"显示为*".GET 工作正常,只是 POST 会出现此错误.可能出什么问题了?如果相关,对于前端,我使用 react 并通过 axios 请求.

which shows "Access-Control-Allow-Origin" as "*". GET works fine, it's just POST that gives this error. What could be going wrong? If relevant, for the frontend I'm using react and requesting through axios.

推荐答案

你必须添加 CORS(app, resources={r"/*": {"origins": "*"}}) 到您的烧瓶应用中.

You have to add CORS(app, resources={r"/*": {"origins": "*"}}) into your flask app.

希望能解决问题.

这篇关于Flask-CORS 不适用于 POST,但适用于 GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:24