falcon Web在本地主机上违反了同源策略

2024-06-25 06:12:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在通过localhost:8000上的elm反应堆运行elm前端。它应该通过localhost:8010上的gunicorn从一个falcon后端加载json文件。这失败了。在

前端可以加载由elm reactor(:8000)提供服务的静态虚拟文件,但当我尝试用实际的后端(:8010)替换这些虚拟文件时,由于缺少标头而失败:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8010/api/sheets. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

来自Firefox检查器的错误消息似乎相当清楚,但我不知道如何修复它。我已经在falcon安装了一个CORS中间件,但这根本没有改善情况。在

from falcon_cors import CORS
cors = CORS(allow_origins_list=['*'])
api = falcon.API(middleware=[cors.middleware])

我也尝试过使用起源'localhost:8000'和{},但都不起作用。在

你知道怎么解决这个问题吗?在


Tags: 文件apijsonlocalhost静态originmiddlewaregunicorn
2条回答

试试这个。希望这能解决你的问题。在

import falcon
from falcon_cors import CORS
cors = CORS(allow_origins_list=['http://localhost:8080', 'http://localhost:8000', 'http://localhost:8010'], allow_all_headers=True, allow_methods_list=['GET', 'POST', 'OPTIONS'])
api = falcon.API(middleware=[cors.middleware])

原来falcon_cors提供了allow_all_origins=True作为参数。这解决了我的问题,但不是一个完美的解决方案。在

当使用POST请求时,也应该设置allow_all_methods=True。在

相关问题 更多 >