{"id":551,"date":"2025-12-06T16:12:01","date_gmt":"2025-12-06T08:12:01","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=551"},"modified":"2025-12-06T16:12:01","modified_gmt":"2025-12-06T08:12:01","slug":"%e5%89%8d%e7%ab%af%e9%aa%8c%e8%af%81%e4%b8%8b%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88npm%e9%aa%8c%e8%af%81","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/06\/%e5%89%8d%e7%ab%af%e9%aa%8c%e8%af%81%e4%b8%8b%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88npm%e9%aa%8c%e8%af%81\/","title":{"rendered":"\u524d\u7aef\u9a8c\u8bc1\u4e0b\u8de8\u57df\u95ee\u9898\u89e3\u51b3\u65b9\u6848(npm\u9a8c\u8bc1)"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd <strong>\u300a\u524d\u7aef\u9a8c\u8bc1\u4e0b\u8de8\u57df\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\uff08\u542b npm \u672c\u5730\u9a8c\u8bc1\u65b9\u5f0f\uff09\u300b<\/strong>\u2014\u2014\u6700\u65b0\u3001\u5b8c\u6574\u3001\u53ef\u76f4\u63a5\u5b9e\u64cd\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u6b63\u5728\u672c\u5730\u5f00\u53d1\u63a5\u53e3\u548c\u524d\u7aef\uff0c\u8fd9\u5957\u6d41\u7a0b\u53ef\u4ee5 <em>\u5feb\u901f\u6a21\u62df\u8de8\u57df<\/em>\u3001<em>\u9a8c\u8bc1\u8de8\u57df\u5931\u8d25\u573a\u666f<\/em>\u3001<em>\u9a8c\u8bc1 CORS \u89e3\u51b3\u65b9\u6848\u662f\u5426\u751f\u6548<\/em>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\ude80 \u4e00\u3001\u524d\u7aef\u9a8c\u8bc1\u8de8\u57df\u95ee\u9898\uff08CORS\uff09\u7684\u539f\u7406<\/h1>\n\n\n\n<p>\u8de8\u57df\u4ea7\u751f\u4e8e\u6d4f\u89c8\u5668 <strong>\u540c\u6e90\u7b56\u7565<\/strong>\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u534f\u8bae\u3001\u57df\u540d\u3001\u7aef\u53e3\u4efb\u610f\u4e00\u4e2a\u4e0d\u540c \u2192 \u90fd\u7b97\u8de8\u57df\u3002<\/p>\n<\/blockquote>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u524d\u7aef<\/th><th>\u540e\u7aef<\/th><th>\u662f\u5426\u8de8\u57df<\/th><\/tr><\/thead><tbody><tr><td><a href=\"http:\/\/localhost:5173\/\">http:\/\/localhost:5173<\/a><\/td><td><a href=\"http:\/\/localhost:3000\/\">http:\/\/localhost:3000<\/a><\/td><td><strong>\u8de8\u57df<\/strong><\/td><\/tr><tr><td><a href=\"http:\/\/abc.com\/\">http:\/\/abc.com<\/a><\/td><td><a href=\"https:\/\/abc.com\/\">https:\/\/abc.com<\/a><\/td><td><strong>\u8de8\u57df<\/strong><\/td><\/tr><tr><td><a href=\"https:\/\/abc.com\/\">https:\/\/abc.com<\/a><\/td><td><a href=\"https:\/\/api.abc.com\/\">https:\/\/api.abc.com<\/a><\/td><td><strong>\u8de8\u57df<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8de8\u57df\u672c\u8d28\u4e0a\u662f\u6d4f\u89c8\u5668\u62e6\u622a\u8bf7\u6c42\uff0c\u4e0d\u662f\u670d\u52a1\u5668\u9519\u8bef\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e8c\u3001\u6700\u5feb\u901f\u9a8c\u8bc1\u8de8\u57df\u9519\u8bef\u7684\u529e\u6cd5\uff08npm \u65b9\u5f0f\uff09<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 \u4f7f\u7528\u4efb\u4f55\u672c\u5730\u524d\u7aef\u670d\u52a1\u5668<\/h2>\n\n\n\n<p>\uff08\u5982 Vite\u3001webpack\u3001http-server\uff09<\/p>\n\n\n\n<p>\u542f\u52a8\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnpm create vite@latest my-app\ncd my-app\nnpm install\nnpm run dev\n\n<\/pre><\/div>\n\n\n<p>\u524d\u7aef \u5730\u5740\u4f8b\u5982\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nhttp:\/\/localhost:5173\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 \u672c\u5730\u968f\u4fbf\u5199\u4e00\u4e2a\u63a5\u53e3\uff08Node Express\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnpm init -y\nnpm i express\n\n<\/pre><\/div>\n\n\n<p>\u5efa\u7acb\uff1a<code>server.js<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst express = require(&quot;express&quot;);\nconst app = express();\n\napp.get(&quot;\/api\/data&quot;, (req, res) =&gt; {\n  res.json({ msg: &quot;hello&quot; });\n});\n\napp.listen(3000, () =&gt; console.log(&quot;API running on 3000&quot;));\n\n<\/pre><\/div>\n\n\n<p>\u542f\u52a8\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnode server.js\n\n<\/pre><\/div>\n\n\n<p>\u540e\u7aef\u5730\u5740\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nhttp:\/\/localhost:3000\n\n<\/pre><\/div>\n\n\n<p>\u8bbf\u95ee\u65f6\u4f1a\u62a5\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nAccess to fetch at &#039;http:\/\/localhost:3000\/api\/data&#039;\nfrom origin &#039;http:\/\/localhost:5173&#039; has been blocked by CORS policy\n\n<\/pre><\/div>\n\n\n<p>\u8fd9\u6837\u4f60 <strong>\u6210\u529f\u7528 npm \u9a8c\u8bc1\u8de8\u57df\u95ee\u9898<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e09\u3001\u89e3\u51b3\u8de8\u57df\u7684 5 \u5927\u65b9\u6cd5\uff08\u9644\u53ef\u8fd0\u884c\u4ee3\u7801\uff09<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u65b9\u6848 1\uff1a\u540e\u7aef\u5141\u8bb8 CORS\uff08\u6700\u4f73\u65b9\u6848\uff09<\/h1>\n\n\n\n<p>\u5b89\u88c5 CORS \u4e2d\u95f4\u4ef6\uff08Express\uff09\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnpm i cors\n\n<\/pre><\/div>\n\n\n<p>\u4f7f\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst cors = require(&quot;cors&quot;);\napp.use(cors());\n\n<\/pre><\/div>\n\n\n<p>\u53ef\u6307\u5b9a\u5141\u8bb8\u7684\u524d\u7aef\u57df\u540d\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napp.use(\n  cors({\n    origin: &quot;http:\/\/localhost:5173&quot;,\n    credentials: true\n  })\n);\n\n<\/pre><\/div>\n\n\n<p>\u8fd9\u662f\u8de8\u57df\u7684\u6700\u6807\u51c6\u3001\u6700\u5b89\u5168\u505a\u6cd5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u65b9\u6848 2\uff1a\u4f7f\u7528 Node \u53cd\u5411\u4ee3\u7406\uff08\u524d\u7aef\u4e0d\u8de8\u57df\uff09<\/h1>\n\n\n\n<p>\uff08\u9002\u7528\u4e8e Vue\u3001React\u3001Vite \u9879\u76ee\uff09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Vite \u914d\u7f6e\u4ee3\u7406\uff1a<\/h3>\n\n\n\n<p><code>vite.config.js<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport default {\n  server: {\n    proxy: {\n      &quot;\/api&quot;: {\n        target: &quot;http:\/\/localhost:3000&quot;,\n        changeOrigin: true,\n        rewrite: path =&gt; path.replace(\/^\\\/api\/, &quot;&quot;)\n      }\n    }\n  }\n};\n\n<\/pre><\/div>\n\n\n<p>\u6240\u6709\u8bf7\u6c42\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nhttp:\/\/localhost:5173\/api\/data\n\n<\/pre><\/div>\n\n\n<p>\u81ea\u52a8\u88ab\u4ee3\u7406\u5230\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nhttp:\/\/localhost:3000\/data\n\n<\/pre><\/div>\n\n\n<p>\u6d4f\u89c8\u5668\u4e0d\u4f1a\u89e6\u53d1\u8de8\u57df\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u65b9\u6848 3\uff1aNginx \u53cd\u5411\u4ee3\u7406\uff08\u751f\u4ea7\u73af\u5883\u5e38\u7528\uff09<\/h1>\n\n\n\n<p><code>nginx.conf<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlocation \/api\/ {\n    proxy_pass http:\/\/localhost:3000\/;\n}\n\n<\/pre><\/div>\n\n\n<p>\u540c\u57df\u8f6c\u53d1\uff0c\u4ece\u6839\u6e90\u4e0a\u89e3\u51b3\u8de8\u57df\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u65b9\u6848 4\uff1aJSONP\uff08\u4ec5 GET\u3001\u8001\u65b9\u6848\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;script&gt;\n  function cb(data) { console.log(data); }\n&amp;lt;\/script&gt;\n\n&amp;lt;script src=&quot;http:\/\/localhost:3000\/jsonp?callback=cb&quot;&gt;&amp;lt;\/script&gt;\n\n<\/pre><\/div>\n\n\n<p>\u540e\u7aef\u8fd4\u56de\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nres.send(`cb(${JSON.stringify(data)})`)\n\n<\/pre><\/div>\n\n\n<p>\u5df2\u4e0d\u63a8\u8350\uff0c\u4f46\u9762\u8bd5\u53ef\u63d0\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u65b9\u6848 5\uff1a\u6d4f\u89c8\u5668\u5173\u95ed\u8de8\u57df\u5b89\u5168\uff08\u4ec5\u7528\u4e8e\u8c03\u8bd5\uff09<\/h1>\n\n\n\n<p>Chrome\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n--disable-web-security --user-data-dir\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f \u4ec5\u9650\u672c\u5730\u8c03\u8bd5\uff0c\u4e0d\u53ef\u7528\u4e8e\u6b63\u5f0f\u73af\u5883\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u56db\u3001\u5982\u4f55\u9a8c\u8bc1\u8de8\u57df\u89e3\u51b3\u662f\u5426\u6210\u529f\uff1f<\/h1>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u7528 npm \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u524d\u7aef\u9875\u9762\uff1a<\/p>\n\n\n\n<p><code>src\/App.vue<\/code>\uff08\u4ee5 Vue \u4e3a\u4f8b\uff09<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;script setup&gt;\nasync function test() {\n  const res = await fetch(&quot;\/api\/data&quot;);\n  console.log(await res.json());\n}\n&amp;lt;\/script&gt;\n\n&amp;lt;template&gt;\n  &amp;lt;button @click=&quot;test&quot;&gt;\u6d4b\u8bd5\u8de8\u57df&amp;lt;\/button&gt;\n&amp;lt;\/template&gt;\n\n<\/pre><\/div>\n\n\n<p>\u70b9\u51fb\u6309\u94ae\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6ca1\u6709\u62a5 CORS \u9519\u8bef<\/strong>\u8bf4\u660e\u8de8\u57df\u95ee\u9898\u89e3\u51b3 \u2714\ufe0f<\/li>\n\n\n\n<li><strong>\u8fd4\u56de\u6b63\u5e38\u6570\u636e<\/strong>\u8bf4\u660e\u4ee3\u7406\u6216 CORS \u8bbe\u7f6e\u6210\u529f \u2714\ufe0f<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e94\u3001\u5bf9\u6bd4\u603b\u7ed3\uff08\u6e05\u6670\u6613\u7406\u89e3\uff09<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6848<\/th><th>\u9002\u7528\u573a\u666f<\/th><th>\u4f18\u70b9<\/th><th>\u7f3a\u70b9<\/th><\/tr><\/thead><tbody><tr><td>\u540e\u7aef CORS<\/td><td>\u6700\u6807\u51c6<\/td><td>\u5b89\u5168\u3001\u6b63\u89c4<\/td><td>\u9700\u540e\u7aef\u914d\u5408<\/td><\/tr><tr><td>\u524d\u7aef\u4ee3\u7406\uff08Vite devServer\uff09<\/td><td>\u672c\u5730\u5f00\u53d1<\/td><td>\u65e0\u8de8\u57df<\/td><td>\u4ec5\u5f00\u53d1\u73af\u5883<\/td><\/tr><tr><td>Nginx \u53cd\u5411\u4ee3\u7406<\/td><td>\u751f\u4ea7\u73af\u5883<\/td><td>\u9ad8\u6027\u80fd\u3001\u7a33\u5b9a<\/td><td>\u8981\u914d\u7f6e\u670d\u52a1\u5668<\/td><\/tr><tr><td>JSONP<\/td><td>\u517c\u5bb9\u65e7\u7cfb\u7edf<\/td><td>\u7b80\u5355<\/td><td>\u53ea\u652f\u6301 GET<\/td><\/tr><tr><td>\u6d4f\u89c8\u5668\u5173\u95ed\u5b89\u5168<\/td><td>\u8c03\u8bd5<\/td><td>\u5feb<\/td><td>\u4e0d\u5b89\u5168<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u597d\uff0c\u6211\u7ed9\u4f60\u6574\u7406\u4e00\u4efd <strong>\u300a\u524d\u7aef\u8de8\u57df\u89e3\u51b3\u65b9\u6848\u5408\u96c6\uff08Vue2\/Vue3\/React + Node.js\/Vite \u5168\u5957\u53ef\u8fd0\u884c\u793a\u4f8b\uff09\u300b<\/strong>\uff0c\u5e76\u9644 npm \u672c\u5730\u9a8c\u8bc1\u6b65\u9aa4\uff0c2025 \u6700\u65b0\u6700\u5168\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\ude80 \u524d\u7aef\u8de8\u57df\u89e3\u51b3\u65b9\u6848\u5408\u96c6\uff082025 \u5b8c\u6574\u7248\uff09<\/h1>\n\n\n\n<p>\u8de8\u57df\u95ee\u9898\u4ea7\u751f\u4e8e\u6d4f\u89c8\u5668\u7684 <strong>\u540c\u6e90\u7b56\u7565<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u534f\u8bae\u3001\u57df\u540d\u3001\u7aef\u53e3\u4efb\u610f\u4e00\u4e2a\u4e0d\u540c \u2192 \u90fd\u7b97\u8de8\u57df<\/li>\n\n\n\n<li>\u524d\u7aef\u8bf7\u6c42\u8de8\u57df\u8d44\u6e90\u4f1a\u88ab\u6d4f\u89c8\u5668\u62e6\u622a\uff0c\u4e0d\u662f\u7f51\u7edc\u9519\u8bef<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e00\u3001\u73af\u5883\u51c6\u5907\uff08npm \u672c\u5730\u9a8c\u8bc1\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 \u524d\u7aef\u9879\u76ee\uff08Vue3 \u793a\u4f8b\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnpm create vite@latest my-app\ncd my-app\nnpm install\nnpm run dev\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u5730\u5740\u793a\u4f8b\uff1a<code>http:\/\/localhost:5173<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 \u540e\u7aef Node.js \u6a21\u62df\u63a5\u53e3<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnpm init -y\nnpm install express cors\n\n<\/pre><\/div>\n\n\n<p><code>server.js<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst express = require(&quot;express&quot;);\nconst cors = require(&quot;cors&quot;);\nconst app = express();\n\n\/\/ \u5141\u8bb8\u6240\u6709\u6765\u6e90\uff08\u53ef\u6539\u4e3a\u7279\u5b9a\u57df\u540d\uff09\napp.use(cors());\n\napp.get(&quot;\/api\/data&quot;, (req, res) =&gt; {\n  res.json({ msg: &quot;hello from server&quot; });\n});\n\napp.listen(3000, () =&gt; console.log(&quot;API running on http:\/\/localhost:3000&quot;));\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3\ufe0f\u20e3 npm \u672c\u5730\u9a8c\u8bc1\u8de8\u57df<\/h3>\n\n\n\n<p>\u524d\u7aef\u8bf7\u6c42\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfetch(&quot;http:\/\/localhost:3000\/api\/data&quot;)\n  .then(res =&gt; res.json())\n  .then(console.log)\n  .catch(console.error);\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5982\u679c\u62a5 CORS \u9519\u8bef<\/strong> \u2192 \u8de8\u57df\u672a\u89e3\u51b3<\/li>\n\n\n\n<li><strong>\u5982\u679c\u8fd4\u56de\u6570\u636e\u6b63\u5e38<\/strong> \u2192 \u8de8\u57df\u5df2\u89e3\u51b3<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e8c\u3001\u8de8\u57df\u89e3\u51b3\u65b9\u6848\u5927\u5168<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 <strong>\u540e\u7aef\u5f00\u542f CORS\uff08\u63a8\u8350\u751f\u4ea7\u73af\u5883\uff09<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst corsOptions = {\n  origin: &quot;http:\/\/localhost:5173&quot;,\n  credentials: true\n};\napp.use(cors(corsOptions));\n\n<\/pre><\/div>\n\n\n<p>\u4f18\u70b9\uff1a\u6807\u51c6\u5b89\u5168\u3001\u7075\u6d3b<br>\u7f3a\u70b9\uff1a\u9700\u8981\u540e\u7aef\u652f\u6301<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 <strong>\u524d\u7aef\u5f00\u53d1\u4ee3\u7406\uff08Vite\/webpack\uff09<\/strong><\/h3>\n\n\n\n<p><code>vite.config.js<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport default {\n  server: {\n    proxy: {\n      &quot;\/api&quot;: {\n        target: &quot;http:\/\/localhost:3000&quot;,\n        changeOrigin: true,\n        rewrite: path =&gt; path.replace(\/^\\\/api\/, &quot;&quot;)\n      }\n    }\n  }\n};\n\n<\/pre><\/div>\n\n\n<p>\u524d\u7aef\u8bf7\u6c42\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfetch(&quot;\/api\/data&quot;).then(...);\n\n<\/pre><\/div>\n\n\n<p>\u4f18\u70b9\uff1a\u5f00\u53d1\u96f6 CORS \u9519\u8bef<br>\u7f3a\u70b9\uff1a\u4ec5\u5f00\u53d1\u73af\u5883<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3\ufe0f\u20e3 <strong>Nginx \u53cd\u5411\u4ee3\u7406\uff08\u751f\u4ea7\u73af\u5883\uff09<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlocation \/api\/ {\n    proxy_pass http:\/\/localhost:3000\/;\n    proxy_set_header Host $host;\n    proxy_set_header X-Real-IP $remote_addr;\n}\n\n<\/pre><\/div>\n\n\n<p>\u4f18\u70b9\uff1a\u9ad8\u6027\u80fd\u3001\u5168\u5c40\u89e3\u51b3<br>\u7f3a\u70b9\uff1a\u9700\u8981\u670d\u52a1\u5668\u914d\u7f6e<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4\ufe0f\u20e3 <strong>JSONP\uff08\u4ec5 GET \u8bf7\u6c42\uff0c\u8001\u65b9\u6848\uff09<\/strong><\/h3>\n\n\n\n<p>\u524d\u7aef\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;script&gt;\n  function cb(data) { console.log(data); }\n&amp;lt;\/script&gt;\n&amp;lt;script src=&quot;http:\/\/localhost:3000\/jsonp?callback=cb&quot;&gt;&amp;lt;\/script&gt;\n\n<\/pre><\/div>\n\n\n<p>\u540e\u7aef\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napp.get(&quot;\/jsonp&quot;, (req, res) =&gt; {\n  const data = { msg: &quot;hello&quot; };\n  res.send(`${req.query.callback}(${JSON.stringify(data)})`);\n});\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f \u5df2\u4e0d\u63a8\u8350\uff0c\u53ea\u80fd GET \u8bf7\u6c42<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5\ufe0f\u20e3 <strong>\u6d4f\u89c8\u5668\u5173\u95ed\u5b89\u5168\uff08\u4ec5\u8c03\u8bd5\uff09<\/strong><\/h3>\n\n\n\n<p>Chrome \u542f\u52a8\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nchrome.exe --disable-web-security --user-data-dir=&quot;C:\/ChromeDev&quot;\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f \u4e0d\u5b89\u5168\uff0c\u4ec5\u7528\u4e8e\u672c\u5730\u6d4b\u8bd5<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e09\u3001Vue3 \u524d\u7aef\u8de8\u57df\u9a8c\u8bc1 Demo<\/h1>\n\n\n\n<p><code>App.vue<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;template&gt;\n  &amp;lt;div&gt;\n    &amp;lt;h1&gt;\u8de8\u57df\u6d4b\u8bd5&amp;lt;\/h1&gt;\n    &amp;lt;button @click=&quot;test&quot;&gt;\u6d4b\u8bd5\u63a5\u53e3&amp;lt;\/button&gt;\n  &amp;lt;\/div&gt;\n&amp;lt;\/template&gt;\n\n&amp;lt;script setup&gt;\nasync function test() {\n  try {\n    const res = await fetch(&quot;\/api\/data&quot;);\n    const data = await res.json();\n    console.log(&quot;\u8fd4\u56de\u6570\u636e:&quot;, data);\n  } catch (err) {\n    console.error(&quot;\u8de8\u57df\u9519\u8bef:&quot;, err);\n  }\n}\n&amp;lt;\/script&gt;\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <strong>Vite \u4ee3\u7406<\/strong> \u6216 <strong>\u540e\u7aef CORS<\/strong> \u540e\uff0c\u70b9\u51fb\u6309\u94ae\u5373\u53ef\u8fd4\u56de\u63a5\u53e3\u6570\u636e\uff0c\u65e0\u8de8\u57df\u62a5\u9519<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u56db\u3001React \u524d\u7aef\u8de8\u57df\u793a\u4f8b<\/h1>\n\n\n\n<p><code>App.jsx<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport { useEffect } from &quot;react&quot;;\n\nfunction App() {\n  const test = async () =&gt; {\n    try {\n      const res = await fetch(&quot;\/api\/data&quot;);\n      const data = await res.json();\n      console.log(&quot;\u8fd4\u56de\u6570\u636e:&quot;, data);\n    } catch (err) {\n      console.error(&quot;\u8de8\u57df\u9519\u8bef:&quot;, err);\n    }\n  };\n\n  return (\n    &amp;lt;div&gt;\n      &amp;lt;h1&gt;\u8de8\u57df\u6d4b\u8bd5&amp;lt;\/h1&gt;\n      &amp;lt;button onClick={test}&gt;\u6d4b\u8bd5\u63a5\u53e3&amp;lt;\/button&gt;\n    &amp;lt;\/div&gt;\n  );\n}\n\nexport default App;\n\n<\/pre><\/div>\n\n\n<p>\u540c\u6837\uff0c\u914d\u5408 Vite \u6216 webpack proxy \u5373\u53ef\u89e3\u51b3\u8de8\u57df\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e94\u3001\u8de8\u57df\u89e3\u51b3\u65b9\u6848\u603b\u7ed3<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6848<\/th><th>\u9002\u7528\u573a\u666f<\/th><th>\u4f18\u70b9<\/th><th>\u7f3a\u70b9<\/th><\/tr><\/thead><tbody><tr><td>\u540e\u7aef CORS<\/td><td>\u6240\u6709\u73af\u5883<\/td><td>\u6807\u51c6\u5b89\u5168<\/td><td>\u540e\u7aef\u9700\u652f\u6301<\/td><\/tr><tr><td>\u524d\u7aef\u4ee3\u7406\uff08Vite\/webpack\uff09<\/td><td>\u5f00\u53d1\u73af\u5883<\/td><td>\u96f6\u8de8\u57df\u9519\u8bef<\/td><td>\u4ec5\u5f00\u53d1\u73af\u5883<\/td><\/tr><tr><td>Nginx \u53cd\u5411\u4ee3\u7406<\/td><td>\u751f\u4ea7\u73af\u5883<\/td><td>\u9ad8\u6027\u80fd\u3001\u7a33\u5b9a<\/td><td>\u9700\u670d\u52a1\u5668\u914d\u7f6e<\/td><\/tr><tr><td>JSONP<\/td><td>\u8001\u7cfb\u7edf\/GET\u8bf7\u6c42<\/td><td>\u7b80\u5355<\/td><td>\u4ec5\u652f\u6301 GET<\/td><\/tr><tr><td>\u6d4f\u89c8\u5668\u5173\u95ed\u5b89\u5168<\/td><td>\u672c\u5730\u8c03\u8bd5<\/td><td>\u5feb\u901f\u9a8c\u8bc1<\/td><td>\u4e0d\u5b89\u5168<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udf81 \u516d\u3001npm \u672c\u5730\u9a8c\u8bc1\u8de8\u57df\u6d41\u7a0b\u603b\u7ed3<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u542f\u52a8\u672c\u5730\u524d\u7aef\uff08Vite\/React\uff09<\/li>\n\n\n\n<li>\u542f\u52a8 Node \u540e\u7aef\u63a5\u53e3<\/li>\n\n\n\n<li>\u524d\u7aef <code>fetch<\/code> \u8bf7\u6c42\u540e\u7aef<\/li>\n\n\n\n<li>\u82e5\u62a5 CORS \u2192 \u5c1d\u8bd5\u540e\u7aef\u5141\u8bb8 CORS \u6216\u524d\u7aef\u4ee3\u7406<\/li>\n\n\n\n<li>\u82e5\u8fd4\u56de\u6b63\u5e38 \u2192 \u8de8\u57df\u95ee\u9898\u89e3\u51b3<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd \u300a\u524d\u7aef\u9a8c\u8bc1\u4e0b\u8de8\u57df\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\uff08\u542b npm \u672c\u5730\u9a8c\u8bc1\u65b9\u5f0f\uff09\u300b\u2014\u2014\u6700&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/06\/%e5%89%8d%e7%ab%af%e9%aa%8c%e8%af%81%e4%b8%8b%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88npm%e9%aa%8c%e8%af%81\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-551","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=551"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/551\/revisions"}],"predecessor-version":[{"id":552,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/551\/revisions\/552"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}