mde*_*dev 8 python cors google-cloud-platform flask-cors google-cloud-functions
可以flask_cors在 Google Cloud Functions 中使用吗?
app = Flask(__name__)
cors = CORS(app)
Run Code Online (Sandbox Code Playgroud)
该flask_cors包在本地可以工作,但部署到 Cloud Functions 上时就不行了。
我尝试了很多不同的方法,正如 GCP 建议的那样https://cloud.google.com/functions/docs/writing/http 但我仍然收到 CORS 错误:
对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。
Dus*_*ram 12
不可以,该app变量在 Cloud Functions 中不可用。
相反,您可以手动处理 CORS:
def cors_enabled_function(request):
# For more information about CORS and CORS preflight requests, see
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
# for more information.
# Set CORS headers for the preflight request
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)
# Set CORS headers for the main request
headers = {
'Access-Control-Allow-Origin': '*'
}
return ('Hello World!', 200, headers)
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅https://cloud.google.com/functions/docs/writing/http#handling_cors_requests 。
| 归档时间: |
|
| 查看次数: |
6397 次 |
| 最近记录: |