亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

啟用 CORS Google Cloud Function (Python)

啟用 CORS Google Cloud Function (Python)

弒天下 2023-09-12 16:29:29
可以flask_cors在 Google Cloud Functions 中使用嗎?app = Flask(__name__) cors = CORS(app)該flask_cors包在本地有效,但部署到云功能上時卻無效。我嘗試了很多不同的方法,正如 GCP 建議的那樣https://cloud.google.com/functions/docs/writing/http 但我仍然收到 CORS 錯誤:對預檢請求的響應未通過訪問控制檢查:請求的資源上不存在“Access-Control-Allow-Origin”標頭。
查看完整描述

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)

查看完整回答
反對 回復 2023-09-12
?
catspeake

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)


查看完整回答
反對 回復 2023-09-12
?
月關寶盒

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


查看完整回答
反對 回復 2023-09-12
?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

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

查看完整回答
反對 回復 2023-09-12
?
鴻蒙傳說

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)

查看完整回答
反對 回復 2023-09-12
  • 5 回答
  • 0 關注
  • 249 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號