Flask-CORS不能用于POST,但为GET工作 [英] Flask-CORS not working for POST, but working for GET
问题描述
我在本地运行Flask-Restful API,并从另一个端口发送包含JSON的POST请求。我收到错误信息
pre $ 否请求的资源上存在Access-Control-Allow-Origin标题。
但是,当我运行
<$ p $访问控制请求方法:POST
- curl --include -X选项http:// localhost:5000 / api / comments / 3
- 头文件Access-Control-Request-Headers:Content-Type
--header来源:http:// localhost:8080
我得到
HTTP / 1.0 200 OK
Content-Type:text / html; charset = utf-8
允许: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。有什么可能会出错?如果相关的话,前端我使用了通过axios的反应和请求。
您必须添加 CORS(app,resources = {r/ *:{origins:*}})
希望解决这个问题。
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.
However, when I run
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
I get
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
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.
You have to add CORS(app, resources={r"/*": {"origins": "*"}})
into your flask app.
Hope that solves the issue.
这篇关于Flask-CORS不能用于POST,但为GET工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!