即使使用 Auth Pre-Flight 的头,使用 Python Falcon 的 CORS 也失败

use*_*800 4 python falcon typescript auth0 angular

在 Angular2 http.get(url, options) 中使用OPTIONS 动词时收到这些错误,即使在 Falcon Rest API 中设置了适当的 CORS 标头。

XMLHttpRequest 无法加载http://localhost:8000/names。预检响应中的 Access-Control-Allow-Headers 不允许请求头字段授权。

resp.set_header("Access-Control-Allow-Origin", "*")
        resp.set_header("Access-Control-Allow-Credentials", "true")
        resp.set_header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT")
        resp.set_header("Access-Control-Allow-Headers",
                       "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers")
Run Code Online (Sandbox Code Playgroud)

对于非 OPTIONS / 普通 http.get() 请求,这可以正常工作。

use*_*800 7

使用 falcon_cors 解决了这个问题,特别是通过设置allow_all_methods=True

pip 安装 falcon-cors

from falcon_cors import CORS

cors = CORS(allow_origins_list=['http://localhost:3000'],
            allow_all_headers=True,
            allow_all_methods=True)

api = falcon.API(middleware=[cors.middleware])
Run Code Online (Sandbox Code Playgroud)