{"id":99302,"date":"2024-04-12T11:36:09","date_gmt":"2024-04-12T03:36:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/99302.html"},"modified":"2024-04-12T11:36:10","modified_gmt":"2024-04-12T03:36:10","slug":"%e8%a7%a3%e5%86%b3angular%e4%b8%ad%e7%9a%84cors%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/99302.html","title":{"rendered":"\u89e3\u51b3Angular\u4e2d\u7684CORS\u95ee\u9898"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/02154242\/9a2f9bff-9f9e-41e8-9ff9-31dcbb16552a.webp\" alt=\"\u89e3\u51b3Angular\u4e2d\u7684CORS\u95ee\u9898\" \/><\/p>\n<p><p>\u5728\u4f7f\u7528Angular\u5f00\u53d1Web\u5e94\u7528\u65f6\uff0c<strong>\u8de8\u6e90\u8d44\u6e90\u5171\u4eab(CORS)\u95ee\u9898\u662f\u5f00\u53d1\u8005\u5e38\u9047\u5230\u7684\u96be\u9898<\/strong>\u3002CORS\u95ee\u9898\u5f80\u5f80\u53d1\u751f\u5728Angular\u5e94\u7528\u5c1d\u8bd5\u4ece\u4e0d\u540c\u57df\u7684\u670d\u52a1\u5668\u8bf7\u6c42\u8d44\u6e90\u65f6\uff0c\u6d4f\u89c8\u5668\u51fa\u4e8e\u5b89\u5168\u8003\u8651\u62e6\u622a\u8fd9\u4e9b\u8bf7\u6c42\u3002\u8981\u89e3\u51b3Angular\u4e2d\u7684CORS\u95ee\u9898\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<strong>\u914d\u7f6e\u4ee3\u7406\u3001\u4fee\u6539\u670d\u52a1\u5668\u7aef\u7684CORS\u8bbe\u7f6e\u3001\u4f7f\u7528JSONP<\/strong>\u3002\u6211\u4eec\u5c06\u4e3b\u8981\u5c55\u5f00\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7<strong>\u914d\u7f6eAngular\u4ee3\u7406<\/strong>\u6765\u7ed5\u8fc7CORS\u9650\u5236\u3002<\/p>\n<\/p>\n<p><p><span style=\"font-weight:bold;color:#333\">\u914d\u7f6eAngular\u4ee3\u7406<\/span>\u662f\u4e00\u79cd\u5e38\u7528\u7684\u89e3\u51b3CORS\u95ee\u9898\u7684\u65b9\u6cd5\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5728Angular\u9879\u76ee\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u4ee3\u7406\u914d\u7f6e\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u544a\u8bc9Angular\u5f00\u53d1\u670d\u52a1\u5668\u5c06\u7279\u5b9a\u7684API\u8bf7\u6c42\u4ee3\u7406\u5230\u53e6\u4e00\u4e2a\u670d\u52a1\u5668\u3002\u8fd9\u6837\uff0c\u6d4f\u89c8\u5668\u5c31\u4f1a\u8ba4\u4e3a\u6240\u6709\u7684\u8bf7\u6c42\u90fd\u662f\u540c\u6e90\u7684\u3002\u63a5\u4e0b\u6765\u7684\u6b63\u6587\u4f1a\u8be6\u7ec6\u9610\u8ff0\u5982\u4f55\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001CORS\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>CORS\u95ee\u9898\u662f\u73b0\u4ee3web\u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u4e86\u89e3\u5176\u80cc\u540e\u7684\u539f\u56e0\u5bf9\u4e8e\u89e3\u51b3\u95ee\u9898\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h4><strong>\u4e3a\u4ec0\u4e48\u4f1a\u6709CORS\u95ee\u9898<\/strong><\/h4>\n<\/p>\n<p><p>CORS\u95ee\u9898\u53d1\u751f\u7684\u6839\u672c\u539f\u56e0\u662fWeb\u6d4f\u89c8\u5668\u7684\u540c\u6e90\u7b56\u7565\u3002\u51fa\u4e8e\u5b89\u5168\u8003\u8651\uff0c\u6d4f\u89c8\u5668\u8981\u6c42Web\u9875\u9762\u4e0a\u7684\u811a\u672c\u5728\u6ca1\u6709\u660e\u786e\u6388\u6743\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u80fd\u4e0e\u53e6\u4e00\u4e2a\u57df\u8fdb\u884c\u4ea4\u4e92\u3002<strong>\u5f53\u53d1\u51fa\u8de8\u57dfHTTP\u8bf7\u6c42\u65f6\uff0c\u4f1a\u5f15\u53d1CORS\u95ee\u9898<\/strong>\u3002<\/p>\n<\/p>\n<p><h4><strong>\u5982\u4f55\u8bc6\u522bCORS\u95ee\u9898<\/strong><\/h4>\n<\/p>\n<p><p>\u901a\u5e38\uff0c\u5f53\u4f60\u5728Angular\u5e94\u7528\u4e2d\u9047\u5230CORS\u95ee\u9898\u65f6\uff0c\u4f1a\u5728\u6d4f\u89c8\u5668\u63a7\u5236\u53f0\u770b\u5230\u7c7b\u4f3c\u4e8e\u201cAccess-Control-Allow-Origin\u201d\u9519\u8bef\u4fe1\u606f\u3002\u8fd9\u5c31\u662fCORS\u9650\u5236\u5bfc\u81f4\u8bf7\u6c42\u88ab\u62e6\u622a\u7684\u76f4\u63a5\u6807\u5fd7\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e8c\u3001\u914d\u7f6e\u4ee3\u7406\u89e3\u51b3CORS<\/h3>\n<\/p>\n<p><p>\u5728Angular\u5e94\u7528\u4e2d\u4f7f\u7528\u4ee3\u7406\u662f\u89e3\u51b3CORS\u7684\u4e00\u4e2a\u6709\u6548\u65b9\u6cd5\u3002\u4ee3\u7406\u80fd\u8ba9API\u8bf7\u6c42\u4ece\u540c\u4e00\u4e2a\u57df\u548c\u7aef\u53e3\u51fa\u53d1\uff0c\u7ed5\u8fc7\u6d4f\u89c8\u5668\u7684\u5b89\u5168\u9650\u5236\u3002<\/p>\n<\/p>\n<p><h4><strong>\u521b\u5efa\u4ee3\u7406\u914d\u7f6e\u6587\u4ef6<\/strong><\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>proxy.conf.json<\/code>\u7684\u4ee3\u7406\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u5728Angular\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u5b58\u653e\u3002\u8be5\u6587\u4ef6\u5305\u542b\u4e86\u5c06\u7279\u5b9aAPI\u8def\u5f84\u6620\u5c04\u5230\u76ee\u6807\u670d\u52a1\u5668\u5730\u5740\u7684\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>  &quot;\/api&quot;: {<\/p>\n<p>    &quot;target&quot;: &quot;http:\/\/your-backend-dom<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.com&quot;,<\/p>\n<p>    &quot;secure&quot;: false,<\/p>\n<p>    &quot;changeOrigin&quot;: true,<\/p>\n<p>    &quot;pathRewrite&quot;: {<\/p>\n<p>      &quot;^\/api&quot;: &quot;&quot;<\/p>\n<p>    }<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u914d\u7f6e\u4e2d\uff0c\u4efb\u4f55\u5305\u542b<code>\/api<\/code>\u7684\u8bf7\u6c42\u90fd\u4f1a\u88ab\u4ee3\u7406\u5230<code>http:\/\/your-backend-domain.com<\/code>\u3002<\/p>\n<\/p>\n<p><h4><strong>\u914d\u7f6eAngular CLI\u4f7f\u7528\u4ee3\u7406<\/strong><\/h4>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u7684<code>angular.json<\/code>\u6587\u4ef6\u4e2d\uff0c\u4e3a\u5f00\u53d1\u670d\u52a1\u5668\u8bbe\u7f6e\u4ee3\u7406\u914d\u7f6e\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">&quot;serve&quot;: {<\/p>\n<p>  &quot;builder&quot;: &quot;@angular-devkit\/build-angular:dev-server&quot;,<\/p>\n<p>  &quot;options&quot;: {<\/p>\n<p>    &quot;proxyConfig&quot;: &quot;src\/proxy.conf.json&quot;<\/p>\n<p>  },<\/p>\n<p>  ...<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53\u4f60\u8fd0\u884c<code>ng serve<\/code>\u547d\u4ee4\u65f6\uff0cAngular CLI\u4f1a\u81ea\u52a8\u8bc6\u522b\u5e76\u5e94\u7528\u8fd9\u4e2a\u4ee3\u7406\u914d\u7f6e\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e09\u3001\u540e\u7aefCORS\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u670d\u52a1\u5668\u7aef\u8bbe\u7f6e\u6765\u89e3\u51b3CORS\u95ee\u9898\u3002\u8fd9\u6d89\u53ca\u5230\u4fee\u6539\u670d\u52a1\u5668\u4e0a\u7684CORS\u76f8\u5173\u914d\u7f6e\uff0c\u4ee5\u5141\u8bb8\u6765\u81ea\u4e0d\u540c\u6e90\u7684\u7279\u5b9a\u7c7b\u578b\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h4><strong>\u8bbe\u7f6eAccess-Control-Allow-Origin<\/strong><\/h4>\n<\/p>\n<p><p>\u670d\u52a1\u5668\u54cd\u5e94\u4e2d\u7684<code>Access-Control-Allow-Origin<\/code>\u5934\u90e8\u5b57\u6bb5\u662fCORS\u653f\u7b56\u7684\u6838\u5fc3\u3002\u670d\u52a1\u5668\u53ef\u4ee5\u8bbe\u7f6e\u8fd9\u4e2a\u5b57\u6bb5\u6765\u5141\u8bb8\u6765\u81ea\u4e0d\u540c\u57df\u7684\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u8bbe\u7f6e\u4e3a\u5177\u4f53\u57df\u540d\u5141\u8bb8\u8de8\u57df\u8bf7\u6c42<\/p>\n<p>res.setHeader(&#039;Access-Control-Allow-Origin&#039;, &#039;http:\/\/your-angular-domain.com&#039;);<\/p>\n<p>\/\/ \u6216\u8005\u8bbe\u7f6e\u4e3a*\u5141\u8bb8\u6240\u6709\u8de8\u57df\u8bf7\u6c42\uff08\u4e0d\u63a8\u8350\uff0c\u5b89\u5168\u98ce\u9669\uff09<\/p>\n<p>res.setHeader(&#039;Access-Control-Allow-Origin&#039;, &#039;*&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8bbe\u7f6e\u5177\u4f53\u7684\u54cd\u5e94\u5934\u90e8<\/strong>\u5f88\u5173\u952e\uff0c\u8fd9\u5c06\u786e\u4fdd\u7b26\u5408CORS\u653f\u7b56\u7684\u8bf7\u6c42\u53ef\u4ee5\u88ab\u63a5\u53d7\u548c\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><h4><strong>\u5904\u7406\u9884\u68c0\u8bf7\u6c42\uff08Preflight\uff09<\/strong><\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u7b80\u5355\u8bf7\u6c42\uff0c\u590d\u6742\u8bf7\u6c42\u4f1a\u5728\u6b63\u5f0f\u4ea4\u4e92\u4e4b\u524d\uff0c\u53d1\u9001\u4e00\u4e2a\u9884\u68c0\u8bf7\u6c42\uff08OPTIONS\u8bf7\u6c42\uff09\u3002\u670d\u52a1\u5668\u7aef\u9700\u8981\u6b63\u786e\u5904\u7406\u8fd9\u4e2a\u9884\u68c0\u8bf7\u6c42\uff0c\u5e76\u8bbe\u7f6e\u76f8\u5e94\u7684\u5934\u4fe1\u606f\uff0c\u5982<code>Access-Control-Allow-Methods<\/code>\u548c<code>Access-Control-Allow-Headers<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u5141\u8bb8\u54ea\u4e9bHTTP\u65b9\u6cd5<\/p>\n<p>res.setHeader(&#039;Access-Control-Allow-Methods&#039;, &#039;GET, POST, OPTIONS, PUT, PATCH, DELETE&#039;);<\/p>\n<p>\/\/ \u5141\u8bb8\u54ea\u4e9b\u5934\u90e8\u4fe1\u606f<\/p>\n<p>res.setHeader(&#039;Access-Control-Allow-Headers&#039;, &#039;X-Requested-With,content-type&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<hr>\n<p><h3>\u56db\u3001\u4f7f\u7528JSONP\u89e3\u51b3CORS<\/h3>\n<\/p>\n<p><p>\u5f53\u65e0\u6cd5\u4ece\u670d\u52a1\u5668\u7aef\u63a7\u5236CORS\u8bbe\u7f6e\u65f6\uff0c\u4f7f\u7528JSONP\uff08JSON with Padding\uff09\u53ef\u4ee5\u4f5c\u4e3a\u89e3\u51b3\u65b9\u6848\u4e4b\u4e00\u3002<strong>JSONP\u662f\u4e00\u79cd\u8001\u5f0f\u7684\u8de8\u57df\u6570\u636e\u83b7\u53d6\u65b9\u5f0f<\/strong>\u3002<\/p>\n<\/p>\n<p><h4><strong>JSONP\u7684\u5de5\u4f5c\u539f\u7406<\/strong><\/h4>\n<\/p>\n<p><p>JSONP\u5de5\u4f5c\u539f\u7406\u662f\u5229\u7528<code>&lt;script&gt;<\/code>\u6807\u7b7e\u6ca1\u6709\u8de8\u57df\u9650\u5236\u7684\u7279\u6027\uff0c\u901a\u8fc7\u52a8\u6001\u521b\u5efa\u4e00\u4e2a<code>&lt;script&gt;<\/code>\u5143\u7d20\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u6570\u636e\u3002<strong>\u540e\u7aef\u9700\u8981\u652f\u6301JSONP\u54cd\u5e94<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u524d\u7aef\u901a\u8fc7\u6dfb\u52a0script\u6807\u7b7e\u53d1\u9001\u8bf7\u6c42<\/p>\n<p>var script = document.createElement(&#039;script&#039;);<\/p>\n<p>script.src = &#039;http:\/\/your-backend-domain.com\/api\/data?callback=parseResponse&#039;;<\/p>\n<p>document.head.appendChild(script);<\/p>\n<p>\/\/ \u540e\u7aef\u8fd4\u56de\u8c03\u7528\u6307\u5b9a\u56de\u8c03\u51fd\u6570\u7684\u54cd\u5e94<\/p>\n<p>parseResponse({\/* data here *\/});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4><strong>Angular\u4e2d\u4f7f\u7528JSONP<\/strong><\/h4>\n<\/p>\n<p><p>Angular HttpClient\u5e93\u4e2d\u63d0\u4f9b\u4e86JSONP\u7684\u652f\u6301\uff0c\u80fd\u591f\u66f4\u5bb9\u6613\u5730\u5b9e\u73b0\u5728\u524d\u7aef\u4f7f\u7528JSONP\u53d1\u9001\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-typescript\">\/\/ \u5728Angular\u670d\u52a1\u4e2d\u53d1\u9001JSONP\u8bf7\u6c42<\/p>\n<p>import { HttpClient, JsonpClientBackend } from &#039;@angular\/common\/http&#039;;<\/p>\n<p>constructor(private jsonp: HttpClient, private jsonpBackend: JsonpClientBackend) {}<\/p>\n<p>getData() {<\/p>\n<p>  const url = &#039;http:\/\/your-backend-domain.com\/api\/data?callback=JSONP_CALLBACK&#039;;<\/p>\n<p>  return this.jsonp.jsonp(url, &#039;JSONP_CALLBACK&#039;)<\/p>\n<p>    .toPromise().then(response =&gt; console.log(response));<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<hr>\n<p><h3>\u4e94\u3001\u524d\u540e\u7aef\u534f\u4f5c\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u6700\u7ec8\u5b9e\u73b0CORS\u95ee\u9898\u7684\u7efc\u5408\u89e3\u51b3\uff0c\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u524d\u540e\u7aef\u5de5\u7a0b\u5e08\u9700\u8981\u7d27\u5bc6\u534f\u4f5c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<p><h4><strong>\u6c9f\u901aAPI\u63a5\u53e3\u548cCORS\u9700\u6c42<\/strong><\/h4>\n<\/p>\n<p><p>\u524d\u540e\u7aef\u5f00\u53d1\u8005\u9700\u8981\u660e\u786e\u63a5\u53e3\u7684\u89c4\u8303\u548cCORS\u76f8\u5173\u9700\u6c42\u3002<strong>\u89c4\u5212\u597dAPI\u63a5\u53e3\u7684\u8bbe\u8ba1\uff0c\u5e76\u660e\u786e\u54ea\u4e9b\u63a5\u53e3\u9700\u8981\u652f\u6301\u8de8\u57df\u8bbf\u95ee<\/strong>\u3002<\/p>\n<\/p>\n<p><h4><strong>\u5171\u540c\u68c0\u67e5\u548c\u6d4b\u8bd5<\/strong><\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u90e8\u7f72\u548c\u6d4b\u8bd5\u73af\u8282\uff0c\u524d\u540e\u7aef\u9700\u8981\u5171\u540c\u53c2\u4e0e\uff0c\u68c0\u67e5CORS\u8bbe\u7f6e\u662f\u5426\u751f\u6548\uff0c\u786e\u4fdd\u6570\u636e\u80fd\u591f\u987a\u5229\u4ea4\u6362\u3002<strong>\u4f7f\u7528\u5f00\u53d1\u8005\u5de5\u5177\u3001Postman\u7b49\u5de5\u5177\u8fdb\u884c\u63a5\u53e3\u6d4b\u8bd5\uff0c\u9a8c\u8bc1CORS\u914d\u7f6e\u662f\u5426\u6b63\u786e<\/strong>\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6709\u6548\u89e3\u51b3\u5728Angular\u4e2d\u9047\u5230\u7684CORS\u95ee\u9898\uff0c\u786e\u4fdd\u5e94\u7528\u80fd\u591f\u6b63\u786e\u8bf7\u6c42\u5e76\u4f7f\u7528\u6765\u81ea\u4e0d\u540c\u6e90\u7684\u8d44\u6e90\u3002axios<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4e3a\u4ec0\u4e48\u6211\u5728\u4f7f\u7528Angular\u65f6\u4f1a\u9047\u5230CORS\u95ee\u9898\uff1f<\/strong><br \/>\n\u5f53\u4f60\u4f7f\u7528Angular\u505a\u524d\u7aef\u5f00\u53d1\u65f6\uff0c\u5982\u679c\u4f60\u7684\u524d\u7aef\u5e94\u7528\u6258\u7ba1\u5728\u4e00\u4e2a\u4e0d\u540c\u7684\u57df\u6216\u7aef\u53e3\u4e0a\uff0c\u800c\u4f60\u7684\u540e\u7aefAPI\u53c8\u5728\u53e6\u4e00\u4e2a\u57df\u6216\u7aef\u53e3\u4e0a\uff0c\u5c31\u4f1a\u51fa\u73b0CORS\u95ee\u9898\u3002\u8fd9\u662f\u7531\u4e8e\u6d4f\u89c8\u5668\u7684\u540c\u6e90\u7b56\u7565\u6240\u5bfc\u81f4\u7684\uff0c\u5b83\u4f1a\u963b\u6b62\u4f60\u7684\u524d\u7aef\u5e94\u7528\u76f4\u63a5\u8bbf\u95ee\u4e0d\u540c\u57df\u7684\u540e\u7aefAPI\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u89e3\u51b3Angular\u4e2d\u7684CORS\u95ee\u9898\uff1f<\/strong><br \/>\n\u8981\u89e3\u51b3Angular\u4e2d\u7684CORS\u95ee\u9898\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4f9b\u9009\u62e9\u3002\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u5728\u4f60\u7684\u540e\u7aefAPI\u4e0a\u914d\u7f6eCORS\u5934\u4fe1\u606f\uff0c\u5141\u8bb8\u4f60\u7684\u524d\u7aef\u5e94\u7528\u8de8\u57df\u8bbf\u95eeAPI\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u5c06\u524d\u7aef\u5e94\u7528\u548c\u540e\u7aefAPI\u653e\u5728\u540c\u4e00\u4e2a\u57df\u4e0a\uff0c\u7136\u540e\u901a\u8fc7\u4ee3\u7406\u670d\u52a1\u5668\u6765\u8f6c\u53d1API\u8bf7\u6c42\u3002<\/p>\n<p><strong>3. \u6211\u8be5\u5982\u4f55\u914d\u7f6eCORS\u5934\u4fe1\u606f\u6765\u89e3\u51b3Angular\u4e2d\u7684CORS\u95ee\u9898\uff1f<\/strong><br \/>\n\u5982\u679c\u4f60\u7684\u540e\u7aefAPI\u662f\u57fa\u4e8eExpress.js\u6216Node.js\u7684\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528cors\u6a21\u5757\u6765\u914d\u7f6eCORS\u5934\u4fe1\u606f\u3002\u5728\u4f60\u7684\u540e\u7aef\u4ee3\u7801\u4e2d\uff0c\u5bfc\u5165cors\u6a21\u5757\uff0c\u5e76\u5728\u4e2d\u95f4\u4ef6\u4e2d\u4f7f\u7528\u5b83\u6765\u8bbe\u7f6eCORS\u5934\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u6307\u5b9a\u5141\u8bb8\u7684\u6e90\u57df\uff0c\u5141\u8bb8\u7684HTTP\u65b9\u6cd5\uff0c\u5141\u8bb8\u7684\u8bf7\u6c42\u5934\u7b49\u3002\u901a\u8fc7\u914d\u7f6e\u6b63\u786e\u7684CORS\u5934\u4fe1\u606f\uff0c\u4f60\u7684\u524d\u7aef\u5e94\u7528\u5c31\u53ef\u4ee5\u8de8\u57df\u8bbf\u95ee\u540e\u7aefAPI\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u4f7f\u7528Angular\u5f00\u53d1Web\u5e94\u7528\u65f6\uff0c\u8de8\u6e90\u8d44\u6e90\u5171\u4eab(CORS)\u95ee\u9898\u662f\u5f00\u53d1\u8005\u5e38\u9047\u5230\u7684\u96be\u9898\u3002CORS\u95ee\u9898\u5f80\u5f80\u53d1\u751f\u5728 [&hellip;]","protected":false},"author":3,"featured_media":99303,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/99302"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=99302"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/99302\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/99303"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=99302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=99302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=99302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}