【发布时间】:2020-08-25 23:21:24
【问题描述】:
我的 firebase python HTTP 云函数不断抛出 CORS 错误:
Access to fetch at 'https://<project>.cloudfunctions.net/<fn_name>' from origin 'http://localhost:3000' 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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
即使我正在处理预检请求:
def get_user_db_exist(request):
"""Responds to any HTTP request.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text or any set of values that can be turned into a
Response object using
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
"""
if request.method == 'OPTIONS':
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '3600'
}
return ('', 204, headers)
headers = {
'Access-Control-Allow-Origin': '*'
}
print(request)
return (json.dumps({ 'status': 'sucess' }), 200, headers)
我尝试将 Access-Control-Allow-Methods 设置为 'GET'、'POST'、'GET, POST' 无济于事(来自前端的请求是 POST 请求)。
我还尝试创建一个可用的现有函数的副本。虽然现有功能有效,但新创建的副本引发了 CORS 错误。
google cloud function python CORS error No 'Access-Control-Allow-Origin' header is present on the requested resource. 和https://cloud.google.com/functions/docs/writing/http#functions_http_cors-python 的说明无效。
前端是一个使用 firebase sdk 版本 7.14.3 的 React 应用程序。 (它也不适用于v7.14.2)。
【问题讨论】:
-
您确定您的 OPTIONS 代码正在被使用吗?您可以在选项请求中添加一些调试或设置自定义标头以检查您是否在 React 中获得它?没有使用 Flask 的经验,但发现此注释:从 Flask 0.6 开始,OPTIONS 被隐式添加并由标准请求处理处理
-
如果你为你的 Flask 应用发布路由配置也许会有所帮助。
-
如果我错了,请纠正我,但不是在预检请求中使用了 OPTIONS 方法(查看 HTTP 资源允许哪些选项)?我也没有用flask app,前端是React,数据库是firebase,所以用了python云函数
标签: python firebase cors google-cloud-functions