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

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

Google Cloud Function 不返回我使用 Go 設置的 CORS 標頭

Google Cloud Function 不返回我使用 Go 設置的 CORS 標頭

Go
慕斯王 2022-05-23 16:31:26
我意識到有類似的問題(例如Google Cloud Functions enable CORS?),但他們的答案似乎對我不起作用。Google Cloud Function 具有以下響應代碼:func HelloWorld(w http.ResponseWriter, r *http.Request) {    [...]    response := make(map[string]interface{})    w.WriteHeader(http.StatusOK)    w.Header().Set("Content-Type", "application/json")    w.Header().Set("Access-Control-Allow-Origin", "*")    w.Header().Set("Allow", "GET, OPTIONS")    w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")    w.Header().Set("Access-Control-Allow-Headers", "*")    response["list"] = list    if err = json.NewEncoder(w).Encode(response); err != nil {        fmt.Println(err)    }}通常我認為它就足夠了Access-Control-Allow-Origin", "*",但由于它不起作用,所以我也包括了其他人。當我嘗試時,curl -v "https://us-central1-my-function.cloudfunctions.net/myfunction"我得到以下響應:[...]* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):* old SSL session ID is stale, removing* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!< HTTP/2 200 < content-type: text/plain; charset=utf-8< function-execution-id: ivz4zonw37d1< x-cloud-trace-context: b6929d3ddf88dc102f6f1f069404aeaa;o=1< date: Wed, 25 Mar 2020 20:00:52 GMT< server: Google Frontend[...]當我嘗試從本地 vuejs 應用程序調用云函數時,出現以下錯誤:Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://us-central1-my-function.cloudfunctions.net/myfunction. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
查看完整描述

1 回答

?
元芳怎么了

TA貢獻1798條經驗 獲得超7個贊

這是你的云功能應該有的標準形式。它應該檢查預檢請求發送的 OPTIONS 方法并設置石南花。然后它應該為主要請求發送石南花。

在這里您可以找到更多信息:

// Package http provides a set of HTTP Cloud Functions samples.

package http


import (

        "fmt"

        "net/http"

)


// CORSEnabledFunction is an example of setting CORS headers.

// For more information about CORS and CORS preflight requests, see

// https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request.

func CORSEnabledFunction(w http.ResponseWriter, r *http.Request) {

        // Set CORS headers for the preflight request

        if r.Method == http.MethodOptions {

                w.Header().Set("Access-Control-Allow-Origin", "*")

                w.Header().Set("Access-Control-Allow-Methods", "POST")

                w.Header().Set("Access-Control-Allow-Headers", "Content-Type")

                w.Header().Set("Access-Control-Max-Age", "3600")

                w.WriteHeader(http.StatusNoContent)

                return

        }

        // Set CORS headers for the main request.

        w.Header().Set("Access-Control-Allow-Origin", "*")

        fmt.Fprint(w, "Hello, World!")

}


查看完整回答
反對 回復 2022-05-23
  • 1 回答
  • 0 關注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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