Google Cloud Function 不返回我使用 Go 设置的 CORS 标头

我意识到有类似的问题(例如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).


慕斯王
浏览 110回答 1
1回答

元芳怎么了

这是你的云功能应该有的标准形式。它应该检查预检请求发送的 OPTIONS 方法并设置石南花。然后它应该为主要请求发送石南花。在这里您可以找到更多信息:// Package http provides a set of HTTP Cloud Functions samples.package httpimport (&nbsp; &nbsp; &nbsp; &nbsp; "fmt"&nbsp; &nbsp; &nbsp; &nbsp; "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) {&nbsp; &nbsp; &nbsp; &nbsp; // Set CORS headers for the preflight request&nbsp; &nbsp; &nbsp; &nbsp; if r.Method == http.MethodOptions {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; w.Header().Set("Access-Control-Allow-Origin", "*")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; w.Header().Set("Access-Control-Allow-Methods", "POST")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; w.Header().Set("Access-Control-Allow-Headers", "Content-Type")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; w.Header().Set("Access-Control-Max-Age", "3600")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; w.WriteHeader(http.StatusNoContent)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Set CORS headers for the main request.&nbsp; &nbsp; &nbsp; &nbsp; w.Header().Set("Access-Control-Allow-Origin", "*")&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprint(w, "Hello, World!")}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go