{"id":208297,"date":"2025-07-08T10:10:11","date_gmt":"2025-07-08T02:10:11","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208297\/"},"modified":"2025-07-08T10:10:11","modified_gmt":"2025-07-08T02:10:11","slug":"%e8%a2%ab-cors-%e6%94%bf%e7%ad%96%e9%98%bb%e6%ad%a2%ef%bc%8c%e4%bd%86%e6%a0%87%e5%a4%b4%e5%ad%98%e5%9c%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208297\/","title":{"rendered":"\u201c\u88ab CORS \u653f\u7b56\u963b\u6b62\u201d\uff0c\u4f46\u6807\u5934\u5b58\u5728"},"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>\u201c\u88ab CORS \u653f\u7b56\u963b\u6b62\u201d\uff0c\u4f46\u6807\u5934\u5b58\u5728<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 14:00:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u201c\u88ab CORS \u653f\u7b56\u963b\u6b62\u201d\uff0c\u4f46\u6807\u5934\u5b58\u5728\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u4f7f\u7528 go \u548c <code>fasthttp<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a rest api\uff0c\u5e76\u4f7f\u7528 vue \u521b\u5efa\u4e86\u4e00\u4e2a\u524d\u7aef\u3002\u6bcf\u6b21\u53d1\u51fa api \u8bf7\u6c42\u65f6\uff0c\u6211\u90fd\u4f1a\u6536\u5230\u9519\u8bef <code>access to xmlhttprequest at 'http:\/\/localhost:55555\/auth\/login' from origin 'http:\/\/localhost:8080' has been returned by cors policy: no 'access- control-allow-origin' \u6807\u5934\u51fa\u73b0\u5728\u6211\u7684\u6d4f\u89c8\u5668\u63a7\u5236\u53f0\u4e2d\u8bf7\u6c42\u7684\u8d44\u6e90 <\/code> \u4e0a\u3002\u6211\u552f\u4e00\u4e00\u6b21\u6ca1\u6709\u6536\u5230\u6b64\u9519\u8bef\u7684\u662f <code>\/auth\/check<\/code> \u8def\u7ebf\u3002\u6211\u8fd8\u5411\u540e\u7aef\u53d1\u51fa\u4e86\u4e00\u4e2a options-request \u5e76\u83b7\u53d6\u4e86\u6240\u6709 cors \u6807\u5934\u3002\u5982\u679c\u6211\u53d1\u51fa post \u6216 get \u8bf7\u6c42\uff0c\u6211\u4e0d\u4f1a\u6536\u5230\u5b83\u4eec\uff0c\u4f46\u6211\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\u3002<\/p>\n<p><strong>\u540e\u7aef\uff1a<\/strong><\/p>\n<pre>webrouter := router.new()\n\nauth := webrouter.group(\"\/auth\")\nauth.get(\"\/check\", authcheck)\nauth.post(\"\/login\", authlogin)\nauth.get(\"\/logout\", authcheck)\n\nerr = fasthttp.listenandserve(\":\"+port, func (ctx *fasthttp.requestctx) {\n    ctx.response.header.set(\"access-control-allow-origin\", \"*\")\n    ctx.response.header.set(\"access-control-allow-headers\", \"authorization, content-type, set-cookie, cookie, server\")\n    ctx.response.header.set(\"access-control-allow-methods\", \"post, get, options\")\n    ctx.response.header.set(\"access-control-allow-credentials\", \"false\")\n    webrouter.handler(ctx)\n})\nif err != nil {\n    panic(err)\n}\n<\/pre>\n<p><strong>\u524d\u7aef\u8bf7\u6c42\uff1a<\/strong><\/p>\n<pre>axios.create({\n    baseURL: 'http:\/\/localhost:55555'\n}).post('\/auth\/login', {\n    email: this.email,\n    password: this.password\n}).then(response =&gt; {\n    console.log(response)\n}).catch(err =&gt; {\n    console.log(err)\n})\n<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u89e3\u51b3\u4e86\u6211\u7684\u95ee\u9898\u3002\u6211\u518d\u6b21\u9605\u8bfb\u4e86 fasthttp \u7684\u6587\u6863\uff0c\u53d1\u73b0 <code>ctx.Error()<\/code> \u65b9\u6cd5\u4f1a\u6e05\u9664\u6240\u6709\u6807\u5934\u3002\u76f8\u53cd\uff0c\u6211\u73b0\u5728\u624b\u52a8\u8bbe\u7f6e\u54cd\u5e94\u4ee3\u7801\u548c\u9519\u8bef\u6d88\u606f\uff0c\u4e00\u5207\u6b63\u5e38\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u201c\u88ab CORS \u653f\u7b56\u963b\u6b62\u201d\uff0c\u4f46\u6807\u5934\u5b58\u5728\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208297","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208297"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208297\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}