我曾尝试在错误404等异常处理程序中使用Sanic_CORS,但收到CORS错误Access to XMLHttpRequest at 'http://backend:port/endpoint' from origin 'http://react_app:port' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.有效端点没有问题。
我的错误处理程序
@app.exception(NotFound)
async def ignore_404s(request, exception):
return response.json({'message': "Oops, server error"},
status=404)如何为错误处理程序实现Sanic_CORS?有什么想法吗?
发布于 2020-06-02 01:52:25
一种选择是只将报头添加到响应中。尽管这不是在使用sanic-cors包,但您在这里确实不需要它。
@app.exception(NotFound)
async def ignore_404s(request, exception):
return response.json(
{'message': "Oops, server error"},
status=404,
headers={"Access-Control-Allow-Origin": "*"}
)https://stackoverflow.com/questions/62028771
复制相似问题