5 回答

TA貢獻1777條經驗 獲得超3個贊
不可以,該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)

TA貢獻1111條經驗 獲得超0個贊
APP
云功能中沒有。您可以按照google cloud 文檔中的說明設置 CORS 標頭,并按照在 Flask 中編寫的方式返回 JSON。
下面的示例調用了hello_world
用于 post 請求的函數。它返回 的狀態和標頭CORS
。
from flask import jsonify
def hello_world(request):
? ? request_json = request.get_json()
? ? # 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': 'POST',
? ? ? ? ? ? '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-Methods': 'POST',
? ? ? ? 'Access-Control-Allow-Origin': '*'
? ? }
? ?if request_json and 'labels' in request_json:
? ? ? ? # THIS IS THE PLACE YOU WRITE YOUR CODE.
? ? ? ? # AWLAYS RETURN WITH THE HEADERS AND STATUS
? ? ? ? return (jsonify({"ok": "Great Day 2"}), 200, headers)

TA貢獻1772條經驗 獲得超5個贊
如果您flask
已經在使用,那么最簡單的方法就是使用flask-cors
https://github.com/corydolphin/flask-cors
像下面這樣裝飾你的云函數就完成了。
from flask_cors import cross_origin
@cross_origin()
@json
def fun_function(request):
# enter code here
或者您可以根據需要添加任意數量的功能,如下所示。
from flask_cors import cross_origin
@cross_origin(allowed_methods=['POST'])
@json
def fun_function(request):
# enter code here

TA貢獻1858條經驗 獲得超8個贊
并通過在 cors_enabled_function 的開頭添加預檢請求的 CORS 標頭來解決它(正如達斯汀·英格拉姆(Dustin Ingram)建議的那樣);我留在函數末尾的主請求的 CORS 標頭(這樣包括返回語句中的響應,無論是 JSON、文本等)。換句話說,我將主函數代碼放在預檢請求和主 CORS 請求之間。

TA貢獻1865條經驗 獲得超7個贊
如果您嘗試從前端進行操作Post request并google cloud function遇到CORS. 以下模板必須有效。它對我有用......
#import all required packages
import json
def function_name(request):
? ? if request.method == 'OPTIONS':
? ? ? ? headers = {
? ? ? ? ? ? 'Access-Control-Allow-Origin': '*',? # Allow your function to be called from any domain
? ? ? ? ? ? 'Access-Control-Allow-Methods': 'POST',? # POST or any method type you need to call
? ? ? ? ? ? 'Access-Control-Allow-Headers': 'Content-Type',?
? ? ? ? ? ? 'Access-Control-Max-Age': '3600',
? ? ? ? }
? ? ? ? return ('', 204, headers)
? ? # Set CORS headers for main requests
? ? headers = {
? ? ? ? 'Access-Control-Allow-Origin': '*',
? ? }
? ? # Your code logic goes here
? ? # Return your response
? ? return (json.dumps({'status': 'success'}), 200, headers)
添加回答
舉報