gpt4 book ai didi

python - Flask-CORS 不适用于 POST,但适用于 GET

转载 作者:太空狗 更新时间:2023-10-29 21:09:31 25 4
gpt4 key购买 nike

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

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 请求。

最佳答案

您必须将 CORS(app, resources={r"/*": {"origins": "*"}}) 添加到您的 Flask 应用中。

希望能解决问题。

关于python - Flask-CORS 不适用于 POST,但适用于 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39550920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com