{"id":16992,"date":"2025-04-18T23:24:33","date_gmt":"2025-04-18T15:24:33","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/16992.html"},"modified":"2025-04-18T23:24:33","modified_gmt":"2025-04-18T15:24:33","slug":"web%e5%89%8d%e7%ab%af10-fetch-api%e5%90%8c%e6%ad%a5-%e5%bc%82%e6%ad%a5-%e8%b7%a8%e5%9f%9f%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/16992.html","title":{"rendered":"WEB\u524d\u7aef10- Fetch API(\u540c\u6b65\/\u5f02\u6b65\/\u8de8\u57df\u5904\u7406)"},"content":{"rendered":"<h4>Fetch API<\/h4>\n<p>Fetch API \u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u8fdc\u7a0b\u6570\u636e&#xff0c;\u7528\u4e8e\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u53d1\u8d77\u548c\u5904\u7406 HTTP \u8bf7\u6c42\u3002\u5b83\u57fa\u4e8e Promise&#xff0c;\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u800c\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u5904\u7406\u7f51\u7edc\u901a\u4fe1&#xff0c;\u66ff\u4ee3\u4e86\u4f20\u7edf\u7684 XMLHttpRequest\u3002<\/p>\n<ul>\n<li>\n<h5>Promise\u5bf9\u8c61<\/h5>\n<\/li>\n<\/ul>\n<p>Promise \u5bf9\u8c61\u662f JavaScript \u4e2d\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u4e00\u79cd\u673a\u5236&#xff0c;\u5b83\u4ee3\u8868\u4e86\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u6700\u7ec8\u5b8c\u6210\u6216\u5931\u8d25&#xff0c;\u5e76\u4e14\u5176\u7ed3\u679c\u503c\u662f\u53ef\u7528\u7684\u3002<\/p>\n<p>\u57fa\u672c\u6982\u5ff5<\/p>\n<li>\u72b6\u6001&#xff08;State&#xff09;&#xff1a;\n<ul>\n<li>Promise \u53ef\u4ee5\u5904\u4e8e\u4e09\u79cd\u72b6\u6001\u4e4b\u4e00&#xff1a;pending&#xff08;\u8fdb\u884c\u4e2d&#xff09;\u3001fulfilled&#xff08;\u5df2\u6210\u529f&#xff09;\u6216 rejected&#xff08;\u5df2\u5931\u8d25&#xff09;\u3002<\/li>\n<li>\u5f53\u521b\u5efa\u4e00\u4e2a Promise \u5bf9\u8c61\u65f6&#xff0c;\u5b83\u6700\u521d\u662f pending \u72b6\u6001&#xff0c;\u968f\u540e\u53ef\u80fd\u53d8\u4e3a fulfilled \u6216 rejected\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u503c&#xff08;Value&#xff09;&#xff1a;\n<ul>\n<li>\u5f53 Promise \u4ece pending \u53d8\u4e3a fulfilled \u65f6&#xff0c;\u4f1a\u4f20\u9012\u4e00\u4e2a\u503c\u4f5c\u4e3a\u64cd\u4f5c\u6210\u529f\u7684\u7ed3\u679c\u3002<\/li>\n<li>\u5f53 Promise \u4ece pending \u53d8\u4e3a rejected \u65f6&#xff0c;\u4f1a\u4f20\u9012\u4e00\u4e2a\u539f\u56e0&#xff08;\u901a\u5e38\u662f Error \u5bf9\u8c61&#xff09;\u4f5c\u4e3a\u64cd\u4f5c\u5931\u8d25\u7684\u539f\u56e0\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u5904\u7406\u5f02\u6b65\u64cd\u4f5c&#xff1a;\n<ul>\n<li>Promise \u5bf9\u8c61\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u64cd\u4f5c&#xff0c;\u53ef\u4ee5\u901a\u8fc7 .then() \u65b9\u6cd5\u6ce8\u518c\u6210\u529f\u548c\u5931\u8d25\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u6216\u8005\u4f7f\u7528 .catch() \u65b9\u6cd5\u6355\u83b7\u5931\u8d25\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<\/li>\n<ul>\n<li>\n<h5>Fetch\u53d1\u8d77\u7f51\u7edc\u8bf7\u6c42<\/h5>\n<\/li>\n<\/ul>\n<p><span class=\"token function\">fetch<\/span><span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">,<\/span> options<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u8fd4\u56de<\/span><\/p>\n<p>url: \u8868\u793a\u8981\u83b7\u53d6\u8d44\u6e90\u7684 URL \u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>options: &#xff08;\u53ef\u9009&#xff09;\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5305\u542b\u914d\u7f6e fetch \u64cd\u4f5c\u7684\u8bbe\u7f6e&#xff0c;\u6bd4\u5982\u65b9\u6cd5\u3001\u5934\u90e8\u3001\u8bf7\u6c42\u4f53\u3001\u51ed\u636e\u7b49\u3002\u8fd9\u4e2a\u53c2\u6570\u5141\u8bb8\u7cbe\u786e\u8c03\u6574\u8bf7\u6c42\u7684\u7ec6\u8282\u3002<\/p>\n<p>\u8fd4\u56de\u503c&#xff1a;fetch(url, options) \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a Promise \u5bf9\u8c61&#xff0c;\u5b83\u4ee3\u8868\u4e86\u8bf7\u6c42\u7684\u5f02\u6b65\u64cd\u4f5c\u3002\u8fd9\u4e2a Promise \u5bf9\u8c61\u5728\u8bf7\u6c42\u5b8c\u6210\u540e\u4f1aresolve&#xff08;\u89e3\u6790&#xff09;&#xff0c;\u8fd4\u56de\u4e00\u4e2a Response \u5bf9\u8c61&#xff0c;\u6216\u5728\u51fa\u73b0\u7f51\u7edc\u9519\u8bef\u65f6reject&#xff08;\u62d2\u7edd&#xff09;&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\u3002<\/p>\n<p>\u5177\u4f53\u6765\u8bf4&#xff1a;<\/p>\n<p>\u5982\u679c\u8bf7\u6c42\u6210\u529f\u5e76\u4e14\u5f97\u5230\u4e86 <span class=\"token constant\">HTTP<\/span> \u72b6\u6001\u7801\u5728 <span class=\"token number\">200<\/span> \u5230 <span class=\"token number\">299<\/span> \u4e4b\u95f4\u7684\u54cd\u5e94&#xff08;\u5305\u62ec <span class=\"token number\">200<\/span> \u548c <span class=\"token number\">299<\/span>&#xff09;&#xff0c;<span class=\"token template-string\"><span class=\"token template-punctuation string\">&#096;<\/span><span class=\"token string\">fetch()<\/span><span class=\"token template-punctuation string\">&#096;<\/span><\/span> Promise \u7684\u72b6\u6001\u5c06\u4f1a\u662f resolved&#xff0c;\u800c\u4e14 <span class=\"token template-string\"><span class=\"token template-punctuation string\">&#096;<\/span><span class=\"token string\">Response<\/span><span class=\"token template-punctuation string\">&#096;<\/span><\/span> \u5bf9\u8c61\u5c06\u4f5c\u4e3a resolve \u7684\u503c\u4f20\u9012\u3002<\/p>\n<p>\u5982\u679c\u51fa\u73b0\u7f51\u7edc\u9519\u8bef&#xff0c;\u6bd4\u5982\u65e0\u6cd5\u8fde\u63a5\u670d\u52a1\u5668\u6216\u8005\u8bf7\u6c42\u8d85\u65f6&#xff0c;<span class=\"token template-string\"><span class=\"token template-punctuation string\">&#096;<\/span><span class=\"token string\">fetch()<\/span><span class=\"token template-punctuation string\">&#096;<\/span><\/span> Promise \u7684\u72b6\u6001\u5c06\u4f1a\u662f rejected&#xff0c;\u5e76\u4e14\u4f1a\u4f20\u9012\u4e00\u4e2a\u9519\u8bef\u5bf9\u8c61\u7ed9 <span class=\"token template-string\"><span class=\"token template-punctuation string\">&#096;<\/span><span class=\"token string\">.catch()<\/span><span class=\"token template-punctuation string\">&#096;<\/span><\/span> \u65b9\u6cd5\u5904\u7406\u3002<\/p>\n<ul>\n<li>\n<h5>\u63a5\u53d7\u6570\u636e\u7684\u65b9\u5f0f<\/h5>\n<\/li>\n<\/ul>\n<p>**\u540c\u6b65\u65b9\u5f0f&#xff1a;**\u540e\u7eed\u4ee3\u7801\u4e0d\u4f1a\u5728\u7ed3\u679c\u8fd4\u56de\u524d\u6267\u884c<\/p>\n<p><span class=\"token keyword\">const<\/span> response <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">await<\/span> <span class=\"token function\">fetch<\/span><span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">,<\/span> options<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u83b7\u5f97\u54cd\u5e94\u5bf9\u8c61<\/span><\/p>\n<p>\u6ce8\u610f&#xff1a;await \u5173\u952e\u5b57\u5fc5\u987b\u5728\u4e00\u4e2a\u6807\u8bb0\u4e86 async \u7684 function \u5185\u6765\u4f7f\u7528<\/p>\n<p>**\u5f02\u6b65\u65b9\u5f0f&#xff1a;**\u540e\u7eed\u4ee3\u7801\u4e0d\u5fc5\u7b49\u5f85\u7ed3\u679c\u8fd4\u56de\u5c31\u53ef\u4ee5\u6267\u884c<\/p>\n<p><span class=\"token function\">fetch<\/span><span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">,<\/span> options<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">then<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">\u7ed3\u679c<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token operator\">&#8230;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token punctuation\">.<\/span>then \u65b9\u6cd5<span class=\"token operator\">:<\/span><br \/>\n<span class=\"token punctuation\">.<\/span><span class=\"token function\">then<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> \u662f Promise \u5bf9\u8c61\u7684\u65b9\u6cd5&#xff0c;\u63a5\u6536\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u3002\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\u4f1a\u5728 Promise \u5bf9\u8c61\u7684\u72b6\u6001\u53d8\u4e3a resolved \u65f6\u88ab\u8c03\u7528\u3002<br \/>\n\u56de\u8c03\u51fd\u6570\u4e2d\u7684 \u7ed3\u679c \u53c2\u6570\u662f Promise \u5bf9\u8c61\u6210\u529f\u89e3\u6790\u540e\u4f20\u9012\u7ed9 <span class=\"token punctuation\">.<\/span><span class=\"token function\">then<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> \u65b9\u6cd5\u7684\u503c\u3002<\/p>\n<p><span class=\"token parameter\">\u7ed3\u679c<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token operator\">&#8230;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token operator\">:<\/span><br \/>\n\u8fd9\u662f\u4e00\u4e2a\u7bad\u5934\u51fd\u6570&#xff0c;\u6216\u8005\u79f0\u4e3a\u56de\u8c03\u51fd\u6570&#xff0c;\u5b83\u63a5\u6536\u4e00\u4e2a\u53c2\u6570 \u7ed3\u679c&#xff0c;\u8fd9\u4e2a\u53c2\u6570\u662f Promise \u89e3\u6790\u540e\u7684\u503c\u3002<br \/>\n\u5927\u62ec\u53f7 <span class=\"token punctuation\">{<\/span> <span class=\"token operator\">&#8230;<\/span> <span class=\"token punctuation\">}<\/span> \u4e2d\u5305\u542b\u4e86\u5177\u4f53\u8981\u6267\u884c\u7684\u64cd\u4f5c\u6216\u903b\u8f91\u3002<\/p>\n<ul>\n<li>\n<h5>\u8de8\u57df\u95ee\u9898<\/h5>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418152431-68026eaf790bf.png\" alt=\"image-20220814105448882\" \/><\/p>\n<p>\u8de8\u57df\u539f\u56e0&#xff1a;<\/p>\n<p>\u8de8\u57df\u95ee\u9898\u7684\u51fa\u73b0\u4e3b\u8981\u662f\u7531\u4e8e\u6d4f\u89c8\u5668\u7684\u540c\u6e90\u7b56\u7565&#xff08;Same-Origin Policy&#xff09;\u9650\u5236\u6240\u5bfc\u81f4\u7684\u3002\u540c\u6e90\u7b56\u7565\u662f\u4e00\u79cd\u5b89\u5168\u673a\u5236&#xff0c;\u65e8\u5728\u9632\u6b62\u4e00\u4e2a\u7f51\u9875\u4e0a\u7684\u811a\u672c\u83b7\u53d6\u53e6\u4e00\u4e2a\u7f51\u9875\u7684\u6570\u636e\u3002\u5177\u4f53\u6765\u8bf4&#xff0c;\u540c\u6e90\u7b56\u7565\u8981\u6c42\u7f51\u9875\u4e2d\u7684\u6240\u6709\u8d44\u6e90&#xff08;\u5982\u811a\u672c\u3001\u6837\u5f0f\u8868\u548c AJAX \u8bf7\u6c42&#xff09;\u5fc5\u987b\u6765\u81ea\u540c\u4e00\u4e2a\u57df\u3001\u534f\u8bae\u548c\u7aef\u53e3&#xff0c;\u5426\u5219\u5c31\u4f1a\u51fa\u73b0\u8de8\u57df\u95ee\u9898\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5bfc\u81f4\u8de8\u57df\u95ee\u9898\u7684\u4e3b\u8981\u539f\u56e0&#xff1a;<\/p>\n<li>\u4e0d\u540c\u57df\u540d: \u5f53\u6d4f\u89c8\u5668\u53d1\u8d77\u8bf7\u6c42\u7684\u6e90&#xff08;Origin&#xff09;\u4e0e\u8d44\u6e90\u6240\u5728\u670d\u52a1\u5668\u7684\u57df\u540d\u4e0d\u4e00\u81f4\u65f6&#xff0c;\u5c31\u4f1a\u53d1\u751f\u8de8\u57df\u8bf7\u6c42\u3002\u4f8b\u5982&#xff0c;\u4ece http:\/\/domain1.com \u53d1\u8d77\u8bf7\u6c42\u8bbf\u95ee http:\/\/domain2.com \u4e0a\u7684\u8d44\u6e90\u3002<\/li>\n<li>\u4e0d\u540c\u5b50\u57df\u540d: \u5373\u4f7f\u662f\u540c\u4e00\u4e2a\u9876\u7ea7\u57df\u540d&#xff0c;\u4e0d\u540c\u5b50\u57df\u540d\u4e4b\u95f4\u4e5f\u88ab\u89c6\u4e3a\u4e0d\u540c\u7684\u6e90\u3002\u4f8b\u5982&#xff0c;http:\/\/sub1.domain.com \u548c http:\/\/sub2.domain.com \u4e4b\u95f4\u7684\u8bf7\u6c42\u5c31\u5c5e\u4e8e\u8de8\u57df\u8bf7\u6c42\u3002<\/li>\n<li>\u4e0d\u540c\u534f\u8bae: \u5f53\u6d4f\u89c8\u5668\u4e2d\u7684\u9875\u9762\u7531 http:\/\/ \u534f\u8bae\u8bbf\u95ee&#xff0c;\u800c\u8bf7\u6c42\u7684\u8d44\u6e90\u662f\u901a\u8fc7 https:\/\/ \u534f\u8bae\u63d0\u4f9b\u65f6&#xff0c;\u4e5f\u4f1a\u9047\u5230\u8de8\u57df\u95ee\u9898\u3002<\/li>\n<li>\u4e0d\u540c\u7aef\u53e3: \u5373\u4f7f\u662f\u540c\u4e00\u57df\u540d\u4e0b\u7684\u4e0d\u540c\u7aef\u53e3&#xff0c;\u4f8b\u5982\u4ece http:\/\/domain.com:3000 \u8bbf\u95ee http:\/\/domain.com:4000 \u7684\u8d44\u6e90&#xff0c;\u4e5f\u4f1a\u88ab\u6d4f\u89c8\u5668\u89c6\u4e3a\u8de8\u57df\u8bf7\u6c42\u3002<\/li>\n<li>\u5b89\u5168\u7b56\u7565\u9650\u5236: \u9664\u4e86\u540c\u6e90\u7b56\u7565&#xff0c;\u6d4f\u89c8\u5668\u8fd8\u4f1a\u6839\u636e\u5b89\u5168\u7b56\u7565&#xff08;\u5982 Content Security Policy&#xff09;\u5bf9\u8de8\u57df\u8bf7\u6c42\u8fdb\u884c\u9650\u5236\u548c\u9632\u8303&#xff0c;\u8fd9\u4e5f\u53ef\u80fd\u5bfc\u81f4\u8de8\u57df\u95ee\u9898\u7684\u51fa\u73b0\u3002<\/li>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;<\/p>\n<p>1.\u54cd\u5e94\u5934\u89e3\u51b3\u65b9\u6848<\/p>\n<p>\u8de8\u57df\u8bf7\u6c42\u7684\u4e3b\u8981\u673a\u5236\u4e4b\u4e00\u662f\u4f7f\u7528\u54cd\u5e94\u5934\u6765\u8fdb\u884c\u63a7\u5236\u548c\u9a8c\u8bc1\u3002\u5f53\u6d4f\u89c8\u5668\u53d1\u8d77\u8de8\u57df\u8bf7\u6c42\u65f6&#xff0c;\u4f8b\u5982\u4ece http:\/\/localhost:7070 \u5411 http:\/\/localhost:8080 \u53d1\u9001\u8bf7\u6c42&#xff0c;\u6d4f\u89c8\u5668\u4f1a\u5728\u8bf7\u6c42\u4e2d\u643a\u5e26\u4e00\u4e2a Origin \u5934&#xff0c;\u8868\u660e\u8bf7\u6c42\u7684\u6e90\u81ea\u4f55\u5904\u3002<\/p>\n<p>\u670d\u52a1\u5668\u7aef\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e Access-Control-Allow-Origin \u5934\u6765\u54cd\u5e94\u8fd9\u4e2a\u8bf7\u6c42\u3002\u4f8b\u5982&#xff0c;\u5982\u679c\u670d\u52a1\u5668\u5141\u8bb8\u6765\u81ea http:\/\/localhost:7070 \u7684\u8bf7\u6c42\u8bbf\u95ee\u8d44\u6e90&#xff0c;\u53ef\u4ee5\u8bbe\u7f6e\u54cd\u5e94\u5934\u5982\u4e0b&#xff1a;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418152431-68026eafafc4e.png\" alt=\"image-20220814144040703\" \/><\/p>\n<p>response<span class=\"token punctuation\">.<\/span><span class=\"token function\">addHeader<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Access-Control-Allow-Origin&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;http:\/\/localhost:7070&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u8fd9\u6837\u6d4f\u89c8\u5668\u5c31\u53ef\u4ee5\u6839\u636e\u54cd\u5e94\u5934\u786e\u5b9a\u662f\u5426\u5141\u8bb8\u5c06\u54cd\u5e94\u4f20\u9012\u7ed9\u8bf7\u6c42\u7684\u9875\u9762\u3002<\/p>\n<p>2.\u4ee3\u7406\u89e3\u51b3\u65b9\u6848<\/p>\n<p>\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u8de8\u57df\u89e3\u51b3\u65b9\u6848\u662f\u4f7f\u7528\u4ee3\u7406\u3002\u4ee3\u7406\u670d\u52a1\u5668\u5145\u5f53\u5ba2\u6237\u7aef\u548c\u76ee\u6807\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u4e2d\u4ecb&#xff0c;\u5b83\u63a5\u6536\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42&#xff0c;\u7136\u540e\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u76ee\u6807\u670d\u52a1\u5668&#xff0c;\u5e76\u5c06\u76ee\u6807\u670d\u52a1\u5668\u7684\u54cd\u5e94\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418152431-68026eafe5df6.png\" alt=\"image-20220814161532141\" \/><\/p>\n<p>\u5728 Node.js \u7684 Express \u6846\u67b6\u4e2d&#xff0c;\u53ef\u4ee5\u4f7f\u7528 http-proxy-middleware \u4e2d\u95f4\u4ef6\u6765\u8bbe\u7f6e\u4ee3\u7406\u3002\u4e0b\u9762\u662f\u5982\u4f55\u914d\u7f6e\u548c\u4f7f\u7528\u8be5\u4e2d\u95f4\u4ef6\u7684\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<p>\u9996\u5148&#xff0c;\u5b89\u88c5 http-proxy-middleware&#xff1a;<\/p>\n<p>npm install http-proxy-middleware &#8211;save-dev<\/p>\n<p>\u7136\u540e&#xff0c;\u5728 Express \u670d\u52a1\u5668\u7684\u542f\u52a8\u4ee3\u7801\u4e2d\u52a0\u5165\u5982\u4e0b\u914d\u7f6e&#xff1a;<\/p>\n<p>import { createProxyMiddleware } from &#039;http-proxy-middleware&#039;;<br \/>\nconst express &#061; require(&#039;express&#039;);<br \/>\nconst app &#061; express();<\/p>\n<p>\/\/ \u8bbe\u7f6e\u4ee3\u7406&#xff0c;\u5c06\u8bf7\u6c42\u8f6c\u53d1\u5230\u76ee\u6807\u670d\u52a1\u5668<br \/>\napp.use(&#039;\/api&#039;, createProxyMiddleware({ target: &#039;http:\/\/localhost:8080&#039;, changeOrigin: true }));<\/p>\n<p>\/\/ \u5176\u4ed6 Express \u4e2d\u95f4\u4ef6\u548c\u8def\u7531\u8bbe\u7f6e<br \/>\n\/\/ &#8230;<\/p>\n<p>\/\/ \u542f\u52a8 Express \u670d\u52a1\u5668<br \/>\napp.listen(7070, () &#061;&gt; {<br \/>\n  console.log(&#039;Express server is running on http:\/\/localhost:7070&#039;);<br \/>\n});<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4f1a\u628a\u6240\u6709\u4ee5 \/api \u5f00\u5934\u7684\u8bf7\u6c42\u90fd\u4ee3\u7406\u5230 http:\/\/localhost:8080&#xff0c;\u5e76\u4e14\u5c06 Origin \u5934\u8bbe\u7f6e\u4e3a\u5f53\u524d\u7684\u6e90\u3002<\/p>\n<p>\u6700\u540e&#xff0c;\u9700\u8981\u786e\u4fdd\u5728\u5ba2\u6237\u7aef\u7684 Fetch \u8bf7\u6c42\u4e2d\u6b63\u786e\u4f7f\u7528\u4ee3\u7406\u8def\u5f84\u3002\u4f8b\u5982&#xff0c;\u5c06\u4e4b\u524d\u7684 Fetch \u8bf7\u6c42&#xff1a;<\/p>\n<p>const resp &#061; await fetch(&#039;http:\/\/localhost:7070\/api\/students&#039;);<\/p>\n<p>\u4fee\u6539\u4e3a\u4f7f\u7528\u4ee3\u7406\u8def\u5f84&#xff1a;<\/p>\n<p>const resp &#061; await fetch(&#039;\/api\/students&#039;);<\/p>\n<p>\u8fd9\u6837\u5ba2\u6237\u7aef\u4ee3\u7801\u5c31\u4f1a\u81ea\u52a8\u5c06\u8bf7\u6c42\u53d1\u9001\u5230 http:\/\/localhost:7070\/api\/students&#xff0c;\u5e76\u901a\u8fc7\u4ee3\u7406\u8f6c\u53d1\u5230\u76ee\u6807\u670d\u52a1\u5668\u4e0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.1k\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf10\u6b21\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u540c\u6e90\u7b56\u7565\u8981\u6c42\u7f51\u9875\u4e2d\u7684\u6240\u6709\u8d44\u6e90\uff08\u5982\u811a\u672c\u3001\u6837\u5f0f\u8868\u548c AJAX \u8bf7\u6c42\uff09\u5fc5\u987b\u6765\u81ea\u540c\u4e00\u4e2a\u57df\u3001\u534f\u8bae\u548c\u7aef\u53e3\uff0c\u5426\u5219\u5c31\u4f1a\u51fa\u73b0\u8de8\u57df\u95ee\u9898\u3002\u4ee3\u7406\u670d\u52a1\u5668\u5145\u5f53\u5ba2\u6237\u7aef\u548c\u76ee\u6807\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u4e2d\u4ecb\uff0c\u5b83\u63a5\u6536\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\uff0c\u7136\u540e\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u76ee\u6807\u670d\u52a1\u5668\uff0c\u5e76\u5c06\u76ee\u6807\u670d\u52a1\u5668\u7684\u54cd\u5e94\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002Promise \u5bf9\u8c61\u662f JavaScript \u4e2d\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u4e00\u79cd\u673a\u5236\uff0c\u5b83\u4ee3\u8868\u4e86\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u6700\u7ec8\u5b8c\u6210\u6216\u5931\u8d25\uff0c\u5e76\u4e14\u5176\u7ed3\u679c\u503c\u662f\u53ef\u7528\u7684\u3002: \uff08\u53ef\u9009\uff09\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5305\u542b\u914d\u7f6e fetch \u64cd\u4f5c\u7684\u8bbe\u7f6e\uff0c\u6bd4\u5982\u65b9\u6cd5\u3001\u5934\u90e8\u3001\u8bf7\u6c42\u4f53\u3001\u51ed\u636e\u7b49\u3002\u8fd9\u4e2a\u53c2\u6570\u5141\u8bb8\u7cbe\u786e\u8c03\u6574\u8bf7\u6c42\u7684\u7ec6\u8282\u3002_fetch \u540c\u6b65<\/p>\n","protected":false},"author":2,"featured_media":16989,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[186,1219,87,43],"topic":[],"class_list":["post-16992","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-node-js","tag-web","tag-87","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>WEB\u524d\u7aef10- Fetch API(\u540c\u6b65\/\u5f02\u6b65\/\u8de8\u57df\u5904\u7406) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/16992.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WEB\u524d\u7aef10- Fetch API(\u540c\u6b65\/\u5f02\u6b65\/\u8de8\u57df\u5904\u7406) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.1k\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf10\u6b21\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u540c\u6e90\u7b56\u7565\u8981\u6c42\u7f51\u9875\u4e2d\u7684\u6240\u6709\u8d44\u6e90\uff08\u5982\u811a\u672c\u3001\u6837\u5f0f\u8868\u548c AJAX \u8bf7\u6c42\uff09\u5fc5\u987b\u6765\u81ea\u540c\u4e00\u4e2a\u57df\u3001\u534f\u8bae\u548c\u7aef\u53e3\uff0c\u5426\u5219\u5c31\u4f1a\u51fa\u73b0\u8de8\u57df\u95ee\u9898\u3002\u4ee3\u7406\u670d\u52a1\u5668\u5145\u5f53\u5ba2\u6237\u7aef\u548c\u76ee\u6807\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u4e2d\u4ecb\uff0c\u5b83\u63a5\u6536\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\uff0c\u7136\u540e\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u76ee\u6807\u670d\u52a1\u5668\uff0c\u5e76\u5c06\u76ee\u6807\u670d\u52a1\u5668\u7684\u54cd\u5e94\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002Promise \u5bf9\u8c61\u662f JavaScript \u4e2d\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u4e00\u79cd\u673a\u5236\uff0c\u5b83\u4ee3\u8868\u4e86\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u6700\u7ec8\u5b8c\u6210\u6216\u5931\u8d25\uff0c\u5e76\u4e14\u5176\u7ed3\u679c\u503c\u662f\u53ef\u7528\u7684\u3002: \uff08\u53ef\u9009\uff09\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5305\u542b\u914d\u7f6e fetch \u64cd\u4f5c\u7684\u8bbe\u7f6e\uff0c\u6bd4\u5982\u65b9\u6cd5\u3001\u5934\u90e8\u3001\u8bf7\u6c42\u4f53\u3001\u51ed\u636e\u7b49\u3002\u8fd9\u4e2a\u53c2\u6570\u5141\u8bb8\u7cbe\u786e\u8c03\u6574\u8bf7\u6c42\u7684\u7ec6\u8282\u3002_fetch \u540c\u6b65\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/16992.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T15:24:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418152431-68026eaf790bf.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/16992.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/16992.html\",\"name\":\"WEB\u524d\u7aef10- Fetch API(\u540c\u6b65\/\u5f02\u6b65\/\u8de8\u57df\u5904\u7406) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T15:24:33+00:00\",\"dateModified\":\"2025-04-18T15:24:33+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/16992.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/16992.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/16992.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WEB\u524d\u7aef10- Fetch API(\u540c\u6b65\/\u5f02\u6b65\/\u8de8\u57df\u5904\u7406)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WEB\u524d\u7aef10- Fetch API(\u540c\u6b65\/\u5f02\u6b65\/\u8de8\u57df\u5904\u7406) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/16992.html","og_locale":"zh_CN","og_type":"article","og_title":"WEB\u524d\u7aef10- Fetch API(\u540c\u6b65\/\u5f02\u6b65\/\u8de8\u57df\u5904\u7406) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.1k\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf10\u6b21\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u540c\u6e90\u7b56\u7565\u8981\u6c42\u7f51\u9875\u4e2d\u7684\u6240\u6709\u8d44\u6e90\uff08\u5982\u811a\u672c\u3001\u6837\u5f0f\u8868\u548c AJAX \u8bf7\u6c42\uff09\u5fc5\u987b\u6765\u81ea\u540c\u4e00\u4e2a\u57df\u3001\u534f\u8bae\u548c\u7aef\u53e3\uff0c\u5426\u5219\u5c31\u4f1a\u51fa\u73b0\u8de8\u57df\u95ee\u9898\u3002\u4ee3\u7406\u670d\u52a1\u5668\u5145\u5f53\u5ba2\u6237\u7aef\u548c\u76ee\u6807\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u4e2d\u4ecb\uff0c\u5b83\u63a5\u6536\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\uff0c\u7136\u540e\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u76ee\u6807\u670d\u52a1\u5668\uff0c\u5e76\u5c06\u76ee\u6807\u670d\u52a1\u5668\u7684\u54cd\u5e94\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002Promise \u5bf9\u8c61\u662f JavaScript \u4e2d\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u4e00\u79cd\u673a\u5236\uff0c\u5b83\u4ee3\u8868\u4e86\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u6700\u7ec8\u5b8c\u6210\u6216\u5931\u8d25\uff0c\u5e76\u4e14\u5176\u7ed3\u679c\u503c\u662f\u53ef\u7528\u7684\u3002: \uff08\u53ef\u9009\uff09\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5305\u542b\u914d\u7f6e fetch \u64cd\u4f5c\u7684\u8bbe\u7f6e\uff0c\u6bd4\u5982\u65b9\u6cd5\u3001\u5934\u90e8\u3001\u8bf7\u6c42\u4f53\u3001\u51ed\u636e\u7b49\u3002\u8fd9\u4e2a\u53c2\u6570\u5141\u8bb8\u7cbe\u786e\u8c03\u6574\u8bf7\u6c42\u7684\u7ec6\u8282\u3002_fetch \u540c\u6b65","og_url":"https:\/\/www.wsisp.com\/helps\/16992.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T15:24:33+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418152431-68026eaf790bf.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/16992.html","url":"https:\/\/www.wsisp.com\/helps\/16992.html","name":"WEB\u524d\u7aef10- Fetch API(\u540c\u6b65\/\u5f02\u6b65\/\u8de8\u57df\u5904\u7406) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T15:24:33+00:00","dateModified":"2025-04-18T15:24:33+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/16992.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/16992.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/16992.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"WEB\u524d\u7aef10- Fetch API(\u540c\u6b65\/\u5f02\u6b65\/\u8de8\u57df\u5904\u7406)"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/16992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=16992"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/16992\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/16989"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=16992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=16992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=16992"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=16992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}