我在本地运行 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 请求。
原文由 hatooku 发布,翻译遵循 CC BY-SA 4.0 许可协议
在我的例子中,由于内部错误引发了 CORS 错误。一个与 CORS 完全无关的错误,应该返回 500,导致了这个。