{"id":42895,"date":"2024-12-01T12:36:47","date_gmt":"2024-12-01T04:36:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42895\/"},"modified":"2024-12-01T12:36:47","modified_gmt":"2024-12-01T04:36:47","slug":"google-cloud-function-%e4%b8%8d%e8%bf%94%e5%9b%9e%e6%88%91%e4%bd%bf%e7%94%a8-go-%e8%ae%be%e7%bd%ae%e7%9a%84-cors-%e6%a0%87%e5%a4%b4","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42895\/","title":{"rendered":"Google Cloud Function \u4e0d\u8fd4\u56de\u6211\u4f7f\u7528 Go \u8bbe\u7f6e\u7684 CORS \u6807\u5934"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>Google Cloud Function \u4e0d\u8fd4\u56de\u6211\u4f7f\u7528 Go \u8bbe\u7f6e\u7684 CORS \u6807\u5934<\/span><\/p>\n<h1>Google Cloud Function \u4e0d\u8fd4\u56de\u6211\u4f7f\u7528 Go \u8bbe\u7f6e\u7684 CORS \u6807\u5934<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 10:06:51<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u7c73\u4e91\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGoogle Cloud Function \u4e0d\u8fd4\u56de\u6211\u4f7f\u7528 Go \u8bbe\u7f6e\u7684 CORS \u6807\u5934\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u610f\u8bc6\u5230\u5b58\u5728\u7c7b\u4f3c\u7684\u95ee\u9898\uff08\u4f8b\u5982 google cloud functions \u542f\u7528 cors\uff1f\uff09\uff0c\u4f46\u4ed6\u4eec\u7684\u7b54\u6848\u4f3c\u4e4e\u5bf9\u6211\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n<p>google cloud function \u5177\u6709\u4ee5\u4e0b\u54cd\u5e94\u4ee3\u7801\uff1a<\/p>\n<pre>func helloworld(w http.responsewriter, r *http.request) {\n    [...]\n    response := make(map[string]interface{})\n    w.writeheader(http.statusok)\n    w.header().set(\"content-type\", \"application\/json\")\n    w.header().set(\"access-control-allow-origin\", \"*\")\n    w.header().set(\"allow\", \"get, options\")\n    w.header().set(\"access-control-allow-methods\", \"get, options\")\n    w.header().set(\"access-control-allow-headers\", \"*\")\n    response[\"list\"] = list\n    if err = json.newencoder(w).encode(response); err != nil {\n        fmt.println(err)\n    }\n}<\/pre>\n<p>\u901a\u5e38\u6211\u8ba4\u4e3a\u4f7f\u7528 <code>access-control-allow-origin\", \"*\"<\/code> \u5c31\u8db3\u591f\u4e86\uff0c\u4f46\u7531\u4e8e\u5b83\u4e0d\u8d77\u4f5c\u7528\uff0c\u6240\u4ee5\u6211\u4e5f\u5305\u62ec\u4e86\u5176\u4ed6\u7684\u3002<\/p>\n<p>\u5f53\u6211\u5c1d\u8bd5 <code>curl -v \"https:\/\/us-central1-my-function.cloudfunctions.net\/myfunction\"<\/code> \u65f6\uff0c\u6211\u5f97\u5230\u4ee5\u4e0b\u54cd\u5e94\uff1a<\/p>\n<pre>[...]\n* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):\n* old SSL session ID is stale, removing\n* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!\n&lt; HTTP\/2 200 \n&lt; content-type: text\/plain; charset=utf-8\n&lt; function-execution-id: ivz4zonw37d1\n&lt; x-cloud-trace-context: b6929d3ddf88dc102f6f1f069404aeaa;o=1\n&lt; date: Wed, 25 Mar 2020 20:00:52 GMT\n&lt; server: Google Frontend\n[...]<\/pre>\n<p>\u5f53\u6211\u5c1d\u8bd5\u4ece\u672c\u5730 vuejs \u5e94\u7528\u7a0b\u5e8f\u8c03\u7528\u4e91\u51fd\u6570\u65f6\uff0c\u51fa\u73b0\u4ee5\u4e0b\u9519\u8bef\uff1a<code>cross-origin request blocked: the same origin policy disallowsread the remote resource at https:\/\/us-central1-my- function.cloudfunctions.net\/myfunction\u3002 \uff08\u539f\u56e0\uff1a\u7f3a\u5c11 cors \u6807\u5934\u201caccess-control-allow-origin\u201d\uff09.<\/code><\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u662f\u60a8\u7684\u4e91\u51fd\u6570\u5e94\u8be5\u5177\u6709\u7684\u6807\u51c6\u5f62\u5f0f\u3002\u5b83\u5e94\u8be5\u68c0\u67e5\u9884\u68c0\u8bf7\u6c42\u53d1\u9001\u7684 options \u65b9\u6cd5\u5e76\u8bbe\u7f6e heathers\u3002\u7136\u540e\u5b83\u5e94\u8be5\u53d1\u9001\u4e3b\u8981\u8bf7\u6c42\u7684\u77f3\u5357\u82b1\u3002<\/p>\n<p>\u5728\u8fd9\u91cc\u60a8\u53ef\u4ee5\u627e\u5230\u66f4\u591a\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<pre>\/\/ Package http provides a set of HTTP Cloud Functions samples.\npackage http\n\nimport (\n        \"fmt\"\n        \"net\/http\"\n)\n\n\/\/ CORSEnabledFunction is an example of setting CORS headers.\n\/\/ For more information about CORS and CORS preflight requests, see\n\/\/ https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Preflight_request.\nfunc CORSEnabledFunction(w http.ResponseWriter, r *http.Request) {\n        \/\/ Set CORS headers for the preflight request\n        if r.Method == http.MethodOptions {\n                w.Header().Set(\"Access-Control-Allow-Origin\", \"*\")\n                w.Header().Set(\"Access-Control-Allow-Methods\", \"POST\")\n                w.Header().Set(\"Access-Control-Allow-Headers\", \"Content-Type\")\n                w.Header().Set(\"Access-Control-Max-Age\", \"3600\")\n                w.WriteHeader(http.StatusNoContent)\n                return\n        }\n        \/\/ Set CORS headers for the main request.\n        w.Header().Set(\"Access-Control-Allow-Origin\", \"*\")\n        fmt.Fprint(w, \"Hello, World!\")\n}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aGoogle Cloud Function \u4e0d\u8fd4\u56de\u6211\u4f7f\u7528 Go \u8bbe\u7f6e\u7684 CORS \u6807\u5934\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; Google Cloud Function \u4e0d\u8fd4\u56de\u6211\u4f7f\u7528 Go \u8bbe\u7f6e\u7684 CORS \u6807\u5934 Google Cloud Function \u4e0d\u8fd4\u56de\u6211\u4f7f\u7528 Go \u8bbe\u7f6e\u7684 CORS \u6807\u5934 \u6765\u6e90\uff1astackoverflow 2024-04-27 10:06:51 0\u6d4f\u89c8 \u6536\u85cf \u7c73\u4e91\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u300aGoogle Cloud Function \u4e0d\u8fd4\u56de\u6211\u4f7f\u7528 Go \u8bbe\u7f6e\u7684 CORS \u6807\u5934\u300b\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60Golang\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u610f\u8bc6\u5230\u5b58\u5728\u7c7b\u4f3c\u7684\u95ee\u9898\uff08\u4f8b\u5982 google cloud functions \u542f\u7528 cors\uff1f\uff09\uff0c\u4f46\u4ed6\u4eec\u7684\u7b54\u6848\u4f3c\u4e4e\u5bf9\u6211\u4e0d\u8d77\u4f5c\u7528\u3002 google cloud function \u5177\u6709\u4ee5\u4e0b\u54cd\u5e94\u4ee3\u7801\uff1a func helloworld(w http.responsewriter, r *http.request) { [&#8230;] response := make(map[string]interface{}) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42895","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42895"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42895\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}