{"id":45958,"date":"2025-07-30T02:01:15","date_gmt":"2025-07-29T18:01:15","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/45958.html"},"modified":"2025-07-30T02:01:15","modified_gmt":"2025-07-29T18:01:15","slug":"%e5%bf%ab%e9%80%9f%e6%90%ad%e5%bb%ba%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%8cfetch%e8%af%b7%e6%b1%82%e4%bb%8e%e6%9c%8d%e5%8a%a1%e5%99%a8%e8%8e%b7%e5%8f%96%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/45958.html","title":{"rendered":"\u5feb\u901f\u642d\u5efa\u670d\u52a1\u5668\uff0cfetch\u8bf7\u6c42\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e"},"content":{"rendered":"<h2>\u00a01.strapi<\/h2>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u9996\u5148strapi\u662f\u4e00\u4e2aapi\u7ba1\u7406\u7cfb\u7edf&#xff0c;\u53ef\u4ee5\u8ba9\u6211\u4eec\u76f4\u63a5\u7528\u7f51\u9875\u7684\u5f62\u5f0f\u53bb\u5b9a\u4e49\u81ea\u5df1\u7684api&#xff0c;\u5305\u62ec\u8bbe\u7f6e\u6a21\u578b\u548c\u6743\u9650\u7b49\u529f\u80fd\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u9996\u5148\u76f4\u63a5\u5728\u9879\u76ee\u76ee\u5f55\u91cc\u9762\u5b89\u88c5\u5e93\u3002npx create-strapi&#064;latest server &#8211;quickstart\u8fd9\u6837\u5c31\u53ef\u4ee5\u76f4\u63a5\u5728\u9879\u76ee\u76ee\u5f55\u521b\u5efa\u4e00\u4e2a\u8fde\u63a5\u6570\u636e\u5e93\u7684\u670d\u52a1\u5668\u4e86\u3002\u4e0d\u7528\u6211\u4eec\u53bb\u4e66\u5199\u4ee3\u7801&#xff0c;\u76f4\u63a5\u5728\u7f51\u9875\u7248\u6570\u636e\u5e93\u6dfb\u52a0\u8868\u4ee5\u53ca\u8868\u4e2d\u7684\u6570\u636e\u5c31\u53ef\u4ee5\u81ea\u52a8\u751f\u6210api\u8ba9\u6211\u4eec\u53bb\u8bbf\u95ee\u8bf7\u6c42\u4e86\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"411\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729180112-68890c68a8caf.png\" width=\"973\" \/><\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5c31\u662f\u8fd9\u79cd&#xff0c;\u6211\u4eec\u7f51\u9875\u5f62\u5f0f\u521b\u5efa\u8868&#xff0c;\u6dfb\u52a0\u6743\u9650&#xff0c;\u81ea\u52a8\u751f\u6210api\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u524d\u7aef\u53d1\u9001\u8bf7\u6c42\u53bb\u83b7\u53d6\u6570\u636e\u4e86\u3002\u503c\u5f97\u4e00\u63d0\u7684\u662f\u8bb0\u5f97npm\u7684\u7248\u672c\u4ee5\u53canode.js\u7248\u672c\u4f1a\u6709\u9650\u5236\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u53ef\u4ee5\u88c5\u4e00\u4e2anvm\u7ba1\u7406\u81ea\u5df1\u5f00\u53d1\u9879\u76ee\u7528\u7684node\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u6211\u662fmac\u7cfb\u7edf\u6240\u4ee5\u7528brew\u5b89\u88c5nvm brem install nvm&#xff0c;\u7136\u540e\u521b\u5efanvm\u5de5\u4f5c\u76ee\u5f55 mkdir ~\/.nvm<\/p>\n<p>\u7136\u540e\u914d\u7f6e\u7ec8\u7aef\u73af\u5883\u53d8\u91cf&#xff0c;nano ~\/.zshrc \u7136\u540e\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>Maestro CLI<br \/>\nexport PATH&#061;&#034;$HOME\/.maestro\/bin:$PATH&#034;<\/p>\n<p>OpenJDK 17<br \/>\nexport PATH&#061;&#034;\/opt\/homebrew\/opt\/openjdk&#064;17\/bin:$PATH&#034;<\/p>\n<p>nvm \u914d\u7f6e&#xff08;Homebrew \u8def\u5f84&#xff0c;Apple M \u82af\u7247&#xff09;<br \/>\nexport NVM_DIR&#061;&#034;$HOME\/.nvm&#034;<br \/>\n[ -s &#034;\/opt\/homebrew\/opt\/nvm\/nvm.sh&#034; ] &amp;&amp; \\\\. &#034;\/opt\/homebrew\/opt\/nvm\/nvm.sh&#034;<br \/>\n[ -s &#034;\/opt\/homebrew\/opt\/nvm\/etc\/bash_completion.d\/nvm&#034; ] &amp;&amp; \\\\. &#034;\/opt\/homebrew\/opt\/nvm\/etc\/bash_completion.d\/nvm&#034;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u7136\u540econtrol x\u9000\u51fa \u6309y \u518d\u6309\u56de\u8f66\u4fdd\u5b58\u9000\u51fa\u6210\u529f\u3002\u5728\u6267\u884c\u7acb\u5373\u751f\u6548\u547d\u4ee4 source ~\/.zshrc\u8fd9\u6837nvm\u5c31\u914d\u7f6e\u597d\u4e86\u3002\u6211\u4eec\u7528nvm install 22\u88c5\u4e0a 22\u7248\u672c\u7684node.js\u7136\u540envm use 22\u5c31\u53ef\u4ee5\u521b\u5efastrapi\u9879\u76ee\u4e86\u3002<\/p>\n<h2>2.fetch\u00a0<\/h2>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u4e0a\u9762\u7f51\u9875\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u8def\u7531\u662f\/api\/students,\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u5c1d\u8bd5\u53bb\u7528fetch&#xff08;\u6d4f\u89c8\u5668\u81ea\u5e26\u7684\u53d1\u9001\u8bf7\u6c42\u7684api&#xff0c;\u662f\u6d4f\u89c8\u5668\u63d0\u4f9b\u7684\u5168\u5c40\u51fd\u6570&#xff0c;\u8fd4\u56de\u7684\u4e00\u4e2apromise&#xff09;\u6240\u4ee5\u53ef\u4ee5\u7528\u94fe\u5f0f\u8c03\u7528.then.catch\u56e0\u4e3apromise\u6709\u72b6\u6001&#xff0c;\u8bf7\u6c42\u6210\u529f\u81ea\u52a8\u6267\u884c.then\u5931\u8d25\u4f1a\u81ea\u52a8\u8c03\u7528.catch\u800c\u4e14\u53c2\u6570\u5c31\u662f\u8fd4\u56de\u7684promise\u5b9e\u4f8b\u5bf9\u8c61\u3002<\/p>\n<p>import React, { useEffect } from &#039;react&#039;<\/p>\n<p>export default function App() {<br \/>\n    const [person, setPerson] &#061; React.useState([])<br \/>\n    const [show, setShow] &#061; React.useState(false)<br \/>\n    \/*<br \/>\n        \u4ece\u63a5\u53e3http:\/\/localhost:1337\/api\/students\u4e2d\u52a0\u8f7d\u6570\u636e<br \/>\n        \u7ec4\u4ef6\u4e00\u6e32\u67d3\u5c31\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\u52a0\u8f7d\u6570\u636e<br \/>\n        \u7ec4\u4ef6\u521d\u59cb\u5316\u65f6\u5019\u53d1\u9001\u8bf7\u6c42\u6765\u52a0\u8f7d\u6570\u636e<br \/>\n    *\/<br \/>\n    useEffect(() &#061;&gt; {<br \/>\n        \/\/\u8bf4\u660e\u6570\u636e\u5f00\u59cb\u52a0\u8f7d\u4e86<br \/>\n        setShow(true)<br \/>\n        \/\/\u53d1\u9001\u8bf7\u6c42<br \/>\n        \/\/fetch()\u7528\u6765\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\u52a0\u8f7d\u6570\u636e&#xff0c;Ajax\u7684\u5347\u7ea7\u7248<br \/>\n        \/\/\u9700\u8981\u4e24\u4e2a\u53c2\u6570 1.\u8bf7\u6c42\u5730\u57402.\u8bf7\u6c42\u4fe1\u606f<br \/>\n        \/\/promise\u4e24\u4e2a\u7279\u70b9 \u6210\u529f\u548c\u5931\u8d25\u4e24\u4e2a\u56de\u8c03\u51fd\u6570.then\u6210\u529f\u65f6\u5019\u8c03\u7528 .catch\u5931\u8d25\u65f6\u8c03\u7528<br \/>\n        fetch(&#039;http:\/\/localhost:1337\/api\/students\/&#039;)<br \/>\n            .then((res) &#061;&gt; {<br \/>\n                \/\/response\u8868\u793a\u54cd\u5e94\u4fe1\u606f<br \/>\n                \/\/ console.log(res);<br \/>\n                return res.json();\/\/\u8be5\u65b9\u6cd5\u53ef\u4ee5\u5c06\u54cd\u5e94\u7684json\u76f4\u63a5\u8f6c\u6362\u4e3ajs\u5bf9\u8c61<br \/>\n            }).then((data) &#061;&gt; {<br \/>\n                console.log(data.data);<br \/>\n                \/\/ \u5c06\u52a0\u8f7d\u5230\u7684\u6570\u636e\u8bbe\u7f6e\u8fdbstate\u4e2d<br \/>\n                setPerson(data.data)<br \/>\n                setShow(false)<br \/>\n            })<br \/>\n            .catch(() &#061;&gt; { })<br \/>\n    }, [])<br \/>\n    return (<br \/>\n        &lt;div&gt;<br \/>\n            {show ? (<br \/>\n                &lt;p&gt;\u6b63\u5728\u52a0\u8f7d\u4e2d&#8230;&lt;\/p&gt;<br \/>\n            ) : (<br \/>\n                person.map((item) &#061;&gt; (<br \/>\n                    &lt;div key&#061;{item.id}&gt;<br \/>\n                        {item.name} &#8212; {item.gender} &#8212; {item.age}<br \/>\n                    &lt;\/div&gt;<br \/>\n                ))<br \/>\n            )}<br \/>\n        &lt;\/div&gt;<br \/>\n    )<br \/>\n}<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8fd9\u91cc\u6211\u4eec\u6ca1\u6709\u5199fetch\u53d1\u9001\u8bf7\u6c42\u7684\u7c7b\u578b&#xff0c;\u6240\u4ee5\u662fget\u3002\u8fd9\u91cc\u5c55\u793a\u6570\u636e\u6210\u529f&#xff0c;\u8bf4\u660e\u8bbf\u95ee\u6210\u529f\u4e4b\u540e\u628a\u6570\u636e\u8fd4\u56de\u6765\u4e86&#xff0c;\u4f46\u662f\u7b2c\u4e00\u6b21\u662fjson\u7c7b\u578b<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"145\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729180112-68890c68e0992.png\" width=\"613\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6211\u4eec\u9700\u8981\u53bb\u8f6c\u5316\u6210\u5bf9\u8c61\u3002\u6bd4\u5982\u8fd9\u91cc\u518d\u6b21\u4f7f\u7528.then\u662f\u56e0\u4e3a\u524d\u9762\u7684.then\u628ajson\u8f6c\u5316\u4e3ajs\u5bf9\u8c61\u3002\u7136\u540ereturn\u8fd4\u56de\u7684\u4e5f\u662f\u4e00\u4e2apromise\u5b9e\u4f8b\u5bf9\u8c61\u3002\u6240\u4ee5\u53ef\u4ee5.then&#xff0c;\u62a5\u9519.catch\u6355\u6349\u3002\u00a0<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"131\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729180113-68890c6908148.png\" width=\"497\" \/><\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8fd9\u91cc\u8f6c\u5316\u7684js\u5bf9\u8c61\u6709\u4e24\u4e2a\u5c5e\u6027\u4e00\u4e2a\u662fdata&#xff0c;\u4e00\u4e2a\u662fmeta&#xff08;\u4e0d\u8ba4\u8bc6&#xff09;&#xff0c;\u4f46\u662f\u6211\u4eec\u5c55\u5f00\u786e\u5b9a\u6211\u4eec\u9700\u8981\u7684\u662fdata\u91cc\u9762\u7684\u5bf9\u8c61<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"205\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729180113-68890c6914b95.png\" width=\"1001\" \/>\u00a0<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u6240\u4ee5\u7528setState\u53bb\u5b58\u4f4f\u8fd4\u56de\u6765\u7684\u6570\u636e\u5c31\u53ef\u4ee5\u53bb\u5c55\u793a\u5230\u9875\u9762\u4e0a\u9762\u4e86\u3002\u5c31\u5b9e\u73b0\u4e86\u7b2c\u4e00\u6b21\u7528fetch\u8bf7\u6c42api\u54cd\u5e94\u6570\u636e\u5c55\u793a\u7684\u8fc7\u7a0b\u4e86\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4f46\u662f\u6211\u4eec\u8981\u77e5\u9053\u8fd8\u6709\u4e00\u4e2a.catch\u6211\u4eec\u6ca1\u6709\u53bb\u5199&#xff0c;\u6bd4\u5982\u6211\u4eec\u53bb\u6389\u8def\u7531students\u4e2d\u7684s&#xff0c;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"367\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729180113-68890c692f4b1.png\" width=\"928\" \/>\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"446\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729180113-68890c6966e57.png\" width=\"1025\" \/>\u00a0<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8fd4\u56de404\u4f46\u662f\u95ee\u9898\u662fmap\u4e5f\u5c31\u662fmap\u65b9\u6cd5\u9519\u4e86\u6240\u4ee5\u8bf7\u6c42\u6574\u4e2a\u8fc7\u7a0b\u8fd8\u662f\u6267\u884c\u4e86&#xff1f;\u8fd9\u5f88\u660e\u663e\u4e0d\u5bf9\u3002\u4f46\u662f\u6211\u4eec\u770b\u5230ok\u53d8\u4e3afalse&#xff0c;\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u53bb\u622a\u65ad\u8fd9\u4e2a\u8bf7\u6c42\u8fc7\u7a0b\u3002\u800c\u4e14statusText\u90fd\u53d8\u4e86\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"424\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729180113-68890c699a79a.png\" width=\"950\" \/><\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5bf9\u6bd4\u6211\u4eec\u5c31\u77e5\u9053\u5982\u679c\u9519\u4e86&#xff0c;ok\u548cstatueText\u4f1a\u6539\u53d8&#xff0c;\u90a3\u5c31\u5bf9\u8fd9\u4e9b\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"205\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729180113-68890c69c048e.png\" width=\"622\" \/>\u00a0<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u6bd4\u5982\u8fd9\u91cc&#xff0c;\u8fd9\u6837\u5982\u679c\u9519\u8bef\u5c31\u4e0d\u4f1a\u518d\u6267\u884c\u8bf7\u6c42\u540e\u9762\u7684\u4ee3\u7801\u4e86\u3002\u56e0\u4e3a\u53ea\u6709\u6210\u529f\u624d\u4f1a\u8fd4\u56depromise\u5b9e\u4f8b\u5bf9\u8c61&#xff0c;\u624d\u53ef\u4ee5\u8c03\u7528\u540e\u9762\u7684.then&#xff0c;\u800c\u4e14.catch\u6ca1\u6709\u5185\u5bb9\u6240\u4ee5\u4ec0\u4e48\u90fd\u6ca1\u6709\u3002<\/p>\n<p>import React, { useEffect } from &#039;react&#039;<\/p>\n<p>export default function App() {<br \/>\n    const [person, setPerson] &#061; React.useState([])<br \/>\n    const [show, setShow] &#061; React.useState(false)<br \/>\n    const [err, setError] &#061; React.useState(null)<br \/>\n    \/*<br \/>\n        \u4ece\u63a5\u53e3http:\/\/localhost:1337\/api\/students\u4e2d\u52a0\u8f7d\u6570\u636e<br \/>\n        \u7ec4\u4ef6\u4e00\u6e32\u67d3\u5c31\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\u52a0\u8f7d\u6570\u636e<br \/>\n        \u7ec4\u4ef6\u521d\u59cb\u5316\u65f6\u5019\u53d1\u9001\u8bf7\u6c42\u6765\u52a0\u8f7d\u6570\u636e<br \/>\n    *\/<br \/>\n    useEffect(() &#061;&gt; {<br \/>\n        \/\/\u8bf4\u660e\u6570\u636e\u5f00\u59cb\u52a0\u8f7d\u4e86<br \/>\n        setShow(true)<br \/>\n        \/\/\u53d1\u9001\u8bf7\u6c42<br \/>\n        \/\/fetch()\u7528\u6765\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\u52a0\u8f7d\u6570\u636e&#xff0c;Ajax\u7684\u5347\u7ea7\u7248<br \/>\n        \/\/\u9700\u8981\u4e24\u4e2a\u53c2\u6570 1.\u8bf7\u6c42\u5730\u57402.\u8bf7\u6c42\u4fe1\u606f<br \/>\n        \/\/promise\u4e24\u4e2a\u7279\u70b9 \u6210\u529f\u548c\u5931\u8d25\u4e24\u4e2a\u56de\u8c03\u51fd\u6570.then\u6210\u529f\u65f6\u5019\u8c03\u7528 .satch\u5931\u8d25\u65f6\u8c03\u7528<br \/>\n        fetch(&#039;http:\/\/localhost:1337\/api\/students\/&#039;)<br \/>\n            .then((res) &#061;&gt; {<br \/>\n                \/\/\u5224\u65ad\u662f\u5426\u6b63\u5e38\u8fd4\u56de\u54cd\u5e94\u4fe1\u606f<br \/>\n                if (res.ok) {<br \/>\n                    console.log(res);<br \/>\n                    return res.json()<br \/>\n                }<br \/>\n                \/\/\u4ee3\u7801\u8fd0\u884c\u5230\u8fd9\u91cc \u8bf4\u660e\u6ca1\u6709\u52a0\u8f7d\u5230\u6570\u636e<br \/>\n                \/\/ setShow(false)<br \/>\n                \/\/\u629b\u51fa\u4e00\u4e2a\u9519\u8bef<br \/>\n                throw new Error(&#039;\u6570\u636e\u52a0\u8f7d\u5931\u8d25&#039;)<br \/>\n                \/\/ response\u8868\u793a\u54cd\u5e94\u4fe1\u606f<br \/>\n                \/\/ console.log(res);<br \/>\n                \/\/ return res.json();\/\/\u8be5\u65b9\u6cd5\u53ef\u4ee5\u5c06\u54cd\u5e94\u7684json\u76f4\u63a5\u8f6c\u6362\u4e3ajs\u5bf9\u8c61<br \/>\n            }).then((data) &#061;&gt; {<br \/>\n                \/\/ console.log(data);<br \/>\n                \/\/ \u5c06\u52a0\u8f7d\u5230\u7684\u6570\u636e\u8bbe\u7f6e\u8fdbstate\u4e2d<br \/>\n                setPerson(data.data)<br \/>\n                setShow(false)<br \/>\n            })<br \/>\n            .catch((e) &#061;&gt; {<br \/>\n                \/\/catch\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u7528\u6765\u7edf\u4e00\u5904\u7406\u9519\u8bef<br \/>\n                \/\/catch\u6267\u884c\u8bf4\u660e\u4e0a\u9762\u4ee3\u7801\u51fa\u9519<br \/>\n                setShow(false)<br \/>\n                \/\/ console.log(e);<br \/>\n                console.log(&#039;e&#039;, e)<br \/>\n                setError(e)<br \/>\n            })<br \/>\n    }, [])<br \/>\n    return (<br \/>\n        &lt;div&gt;<br \/>\n            {show &amp;&amp; &lt;p&gt;\u52a0\u8f7d\u4e2d&lt;\/p&gt;}<br \/>\n            {err &amp;&amp; &lt;p&gt;\u51fa\u9519\u4e86&#xff1a;{err.message}&lt;\/p&gt;}<br \/>\n            {!show &amp;&amp; !err &amp;&amp; person.map((item) &#061;&gt; (<br \/>\n                &lt;div key&#061;{item.id}&gt;<br \/>\n                    {item.name} &#8212; {item.gender} &#8212; {item.age}<br \/>\n                &lt;\/div&gt;<br \/>\n            ))}<br \/>\n        &lt;\/div&gt;<br \/>\n    )<br \/>\n}<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5f53\u62a5\u9519\u7684\u65f6\u5019\u7528throw new Error&#xff08;\u2018\u2019&#xff09;\u6254\u51fa\u4e00\u4e2aerr&#xff0c;\u7136\u540e.catch\u5c31\u53ef\u4ee5\u6355\u6349\u5230\u3002\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u6574\u4e2a\u8bf7\u6c42\u7684\u903b\u8f91&#xff0c;\u9996\u5148\u8981\u6709\u4e00\u4e2astate\u63a5\u6536\u54cd\u5e94\u6570\u636e&#xff0c;\u8fd8\u8981\u6709\u4e00\u4e2astate\u53bb\u63a7\u5236\u8bf7\u6c42\u53d1\u9001\u540e\u5982\u679c\u6ca1\u6709\u5373\u4f7f\u54cd\u5e94\u5c55\u793a\u7684\u5185\u5bb9&#xff0c;\u8fd8\u9700\u8981\u4e00\u4e2astate\u53bb\u63a7\u5236\u9875\u9762\u5982\u679c\u62a5\u9519\u4e86\u5c55\u793a\u9519\u8bef\u4fe1\u606f&#xff0c;<\/p>\n<h2>3.async await\u8bed\u6cd5\u7cd6<\/h2>\n<p>import React, { useEffect } from &#039;react&#039;<\/p>\n<p>export default function App() {<br \/>\n    const [person, setPerson] &#061; React.useState([])<br \/>\n    const [show, setShow] &#061; React.useState(false)<br \/>\n    const [err, setError] &#061; React.useState(null)<br \/>\n    useEffect(() &#061;&gt; {<\/p>\n<p>        const fetData &#061; async () &#061;&gt; {<br \/>\n            try {<br \/>\n                setShow(true)<br \/>\n                setError(null)<br \/>\n                const res &#061; await fetch(&#039;http:\/\/localhost:1337\/api\/students&#039;)<br \/>\n                if (res.ok) {<br \/>\n                    const data &#061; await res.json()<br \/>\n                    setPerson(data.data)<\/p>\n<p>                } else {<br \/>\n                    throw new Error(&#039;\u6570\u636e\u52a0\u8f7d\u5931\u8d25&#xff01;&#039;)<br \/>\n                }<br \/>\n            } catch (e) {<br \/>\n                setError(e)<br \/>\n            } finally {<br \/>\n                setShow(false)<br \/>\n            }<br \/>\n        }<\/p>\n<p>        fetData()<br \/>\n    }, [])<br \/>\n    return (<br \/>\n        &lt;div&gt;<br \/>\n            {show &amp;&amp; &lt;p&gt;\u52a0\u8f7d\u4e2d&lt;\/p&gt;}<br \/>\n            {err &amp;&amp; &lt;p&gt;\u51fa\u9519\u4e86&#xff1a;{err.message}&lt;\/p&gt;}<br \/>\n            {!show &amp;&amp; !err &amp;&amp; person.map((item) &#061;&gt; (<br \/>\n                &lt;div key&#061;{item.id}&gt;<br \/>\n                    {item.name} &#8212; {item.gender} &#8212; {item.age}<br \/>\n                &lt;\/div&gt;<br \/>\n            ))}<br \/>\n        &lt;\/div&gt;<br \/>\n    )<br \/>\n}<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6700\u5f00\u59cb\u6211\u4eec\u4f7f\u7528 fetch \u662f\u901a\u8fc7\u8fde\u7eed\u8c03\u7528 .then \u6765\u4e00\u6b65\u6b65\u83b7\u53d6 response \u548c json \u6570\u636e&#xff0c;\u56e0\u4e3a fetch \u8fd4\u56de\u7684\u662f\u4e00\u4e2a Promise \u5b9e\u4f8b\u5bf9\u8c61&#xff0c;\u4e0d\u80fd\u76f4\u63a5\u53d6\u5230\u91cc\u9762\u7684\u6570\u636e\u3002\u800c\u4f7f\u7528 await \u5173\u952e\u5b57\u540e&#xff0c;\u5c31\u76f8\u5f53\u4e8e\u81ea\u52a8\u5e2e\u6211\u4eec\u8c03\u7528\u4e86 .then&#xff0c;\u628a Promise \u89e3\u6790\u540e\u7684\u6570\u636e\u76f4\u63a5\u8d4b\u503c\u7ed9\u53d8\u91cf\u3002\u6bd4\u5982\u7b2c\u4e00\u6b21\u7528 await fetch&#8230; \u83b7\u53d6\u54cd\u5e94\u5bf9\u8c61 response&#xff0c;\u7b2c\u4e8c\u6b21\u518d\u7528 await response.json() \u628a JSON \u6570\u636e\u89e3\u6790\u6210 JS \u5bf9\u8c61\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7531\u4e8e await \u662f\u540c\u6b65\u8bed\u6cd5\u98ce\u683c&#xff0c;\u6211\u4eec\u5c31\u4e0d\u80fd\u518d\u7528 .catch \u6765\u6355\u83b7\u9519\u8bef&#xff0c;\u800c\u9700\u8981\u4f7f\u7528 try catch finally \u6765\u6355\u83b7\u5f02\u5e38&#xff0c;\u6bd4\u5982 throw \u629b\u51fa\u7684\u9519\u8bef\u624d\u80fd\u5728 catch \u4e2d\u5904\u7406\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff1a;\u53ea\u6709\u5728 async \u58f0\u660e\u7684\u51fd\u6570\u4e2d\u624d\u80fd\u4f7f\u7528 await\u3002<\/p>\n<h2>4.\u5220\u9664\u6570\u636e<\/h2>\n<p>import React, { } from &#039;react&#039;;<br \/>\nimport StuContext from &#039;.\/store\/StuContext&#039;;<br \/>\nimport StudentForm from &#039;.\/StudentForm&#039;;<br \/>\nconst Student &#061; (props) &#061;&gt; {<br \/>\n    const Stx &#061; React.useContext(StuContext)<br \/>\n    const [loading, setLoading] &#061; React.useState(false)<br \/>\n    const [error, setError] &#061; React.useState(null)<br \/>\n    const [isEdit, setIsEdit] &#061; React.useState(null)<br \/>\n    const deleteData &#061; React.useCallback(async () &#061;&gt; {<br \/>\n        try {<br \/>\n            setLoading(true)<br \/>\n            setError(null)<\/p>\n<p>            const res &#061; await fetch(http:\/\/localhost:1337\/api\/students\/${props.stu.documentId}, {<br \/>\n                method: &#039;delete&#039;<br \/>\n            })<br \/>\n            if (!res.ok) {<br \/>\n                throw new Error(&#039;\u5220\u9664\u5931\u8d25&#039;)<br \/>\n            }<br \/>\n            \/\/\u4fee\u6539\u6210\u529f\u540e\u89e6\u53d1\u5217\u8868\u91cd\u65b0\u5237\u65b0<br \/>\n            Stx.fetchData()<br \/>\n            \/\/ const data &#061; await res.json()<br \/>\n            \/\/ console.log(&#039;data&#039;, data)<br \/>\n            \/\/ \u88ab\u5220\u9664\u7684\u5b66\u751f<br \/>\n        } catch (e) {<br \/>\n            console.log(e);<br \/>\n            setError(e)<br \/>\n        } finally {<br \/>\n            setLoading(false)<br \/>\n        }<br \/>\n    }, [])<\/p>\n<p>    const Delete &#061; () &#061;&gt; {<br \/>\n        \/\/\u5220\u9664\u5b66\u751f<br \/>\n        deleteData()<br \/>\n    }<\/p>\n<p>    const cancel &#061; () &#061;&gt; {<br \/>\n        setIsEdit(false)<br \/>\n    }<\/p>\n<p>    return (<br \/>\n        &lt;&gt;<br \/>\n            {!isEdit &amp;&amp;<br \/>\n                &lt;tr&gt;<br \/>\n                    &lt;td&gt;{props.stu.name}&lt;\/td&gt;<br \/>\n                    &lt;td&gt;{props.stu.gender}&lt;\/td&gt;<br \/>\n                    &lt;td&gt;{props.stu.age}&lt;\/td&gt;<br \/>\n                    &lt;td&gt;{props.stu.address}&lt;\/td&gt;<br \/>\n                    &lt;td&gt;<br \/>\n                        &lt;button onClick&#061;{Delete} &gt;\u5220\u9664&lt;\/button&gt;<br \/>\n                    &lt;\/td&gt;<br \/>\n                    &lt;td&gt;<br \/>\n                        &lt;button onClick&#061;{() &#061;&gt; { setIsEdit(true) }} &gt;\u4fee\u6539&lt;\/button&gt;<br \/>\n                    &lt;\/td&gt;<br \/>\n                &lt;\/tr&gt;<br \/>\n            }<br \/>\n            {<br \/>\n                isEdit &amp;&amp; &lt;StudentForm stu&#061;{props.stu} onCancel&#061;{cancel} \/&gt;<br \/>\n            }<br \/>\n            {<br \/>\n                loading &amp;&amp; &lt;tr&gt;&lt;td colSpan&#061;{5}&gt;\u6b63\u5728\u5220\u9664\u6570\u636e&lt;\/td&gt;&lt;\/tr&gt;<br \/>\n            }<br \/>\n            {<br \/>\n                error &amp;&amp; &lt;tr&gt;&lt;td colSpan&#061;{5}&gt;\u5220\u9664\u5931\u8d25&lt;\/td&gt;&lt;\/tr&gt;<br \/>\n            }<br \/>\n        &lt;\/&gt;<\/p>\n<p>    );<br \/>\n};<\/p>\n<p>export default Student;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8fd9\u662f\u6211\u4eec\u7684\u5220\u9664\u8bf7\u6c42&#xff0c;\u6211\u4eec\u53ea\u9700\u8981\u5728\u540e\u9762\u7684\u5bf9\u8c61\u4e2d\u6307\u540d\u8bf7\u6c42\u5934\u4e3adelete\u5c31\u53ef\u4ee5\u53bb\u5220\u9664\u4e86\u3002\u5f53\u7136\u9700\u8981\u4e00\u4e2a\u53c2\u6570documentId\u53bb\u6307\u540d\u5220\u9664\u7684\u54ea\u4e00\u4e2a\u3002id\u4e0d\u884c\u5728\u8bf4\u4e00\u8fb9\u6211\u7528\u7684strapi\u4e0d\u652f\u6301id\u53bb\u6307\u5b9a\u5220\u9664\u6216\u8005\u66f4\u65b0\u7684\u8868\u4e2d\u7684\u5217\u3002\u5361\u4e86\u4e00\u5929\u4e86&#8230;&#8230;&#8230;&#8230;.\u53ea\u4e0d\u8fc7\u4e00\u4e2a\u70b9\u662fContext\u6211\u4eec\u7528\u5230\u4e86&#xff0c;\u56e0\u4e3a\u6211\u4eec<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"105\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729180113-68890c69e9f4c.png\" width=\"601\" \/>\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"204\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729180114-68890c6a0d716.png\" width=\"656\" \/>\u00a0<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u6bcf\u6b21\u5728\u5220\u9664\u4e4b\u540e\u9700\u8981\u91cd\u65b0\u52a0\u8f7d\u4e00\u904d\u6570\u636e&#xff0c;\u4fdd\u8bc1\u6700\u65b0\u7684\u6570\u636e\u5728 \u9875\u9762\u4e0a\u3002<\/p>\n<p>     &lt;StuContext.Provider value&#061;{{ fetchData }}&gt;<br \/>\n            &lt;div className&#061;&#034;app&#034;&gt;<br \/>\n                &lt;button onClick&#061;{() &#061;&gt; { loadDataHandler() }}&gt;\u52a0\u8f7d\u6570\u636e&lt;\/button&gt;<br \/>\n                {(!loading &amp;&amp; !error) &amp;&amp; &lt;StudentList stus&#061;{stuData} \/&gt;}<br \/>\n                {loading &amp;&amp; &lt;p&gt;\u6570\u636e\u6b63\u5728\u52a0\u8f7d\u4e2d&#8230;&lt;\/p&gt;}<br \/>\n                {error &amp;&amp; &lt;p&gt;\u6570\u636e\u52a0\u8f7d\u5f02\u5e38&#xff01;&lt;\/p&gt;}<br \/>\n            &lt;\/div&gt;<br \/>\n        &lt;\/StuContext.Provider&gt;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5c31\u662f\u8fd9\u6837\u7136\u540e\u6211\u4eec\u5728\u5220\u9664\u8bf7\u6c42\u6210\u529f\u4e4b\u540e&#xff0c;\u6267\u884c\u4e00\u4e0b\u91cd\u65b0\u52a0\u8f7d\u7684\u65b9\u6cd5\u5c31\u597d\u4e86\u3002<\/p>\n<h2>5.\u521b\u5efa\u4ee5\u53ca\u66f4\u65b0\u8bf7\u6c42<\/h2>\n<p>import React from &#039;react&#039;<br \/>\nimport &#039;.\/StudentForm.css&#039;<br \/>\nimport StuContext from &#039;.\/store\/StuContext&#039;<br \/>\nexport default function StudentForm(props) {<br \/>\n    const Stx &#061; React.useContext(StuContext)<br \/>\n    const [input, setInput] &#061; React.useState({<br \/>\n        name: props.stu ? props.stu.name : &#039;&#039;, age: props.stu ? props.stu.age : &#039;&#039;,<br \/>\n        address: props.stu ? props.stu.address : &#039;&#039;, gender: props.stu ? props.stu.gender : &#039;\u7537&#039;<br \/>\n    })<br \/>\n    const [loading, setLoading] &#061; React.useState(false)<br \/>\n    const [err, setErr] &#061; React.useState(null)<br \/>\n    const addData &#061; (async (input) &#061;&gt; {<br \/>\n        try {<br \/>\n            setLoading(true)<br \/>\n            setErr(null)<br \/>\n            const res &#061; await fetch(&#039;http:\/\/localhost:1337\/api\/students&#039;, {<br \/>\n                method: &#039;post&#039;,<br \/>\n                body: JSON.stringify({<br \/>\n                    data: input<br \/>\n                }),<br \/>\n                headers: {<br \/>\n                    &#039;Content-Type&#039;: &#039;application\/json&#039;<br \/>\n                }<br \/>\n            })<br \/>\n            console.log(&#039;res.ok&#039;, res.ok)<br \/>\n            if (!res.ok) {<br \/>\n                throw new Error(&#039;\u6dfb\u52a0\u5931\u8d25&#039;)<br \/>\n            }<br \/>\n            \/\/\u6dfb\u52a0\u6210\u529f\u5237\u65b0\u5217\u8868<br \/>\n            Stx.fetchData()<\/p>\n<p>        } catch (e) {<br \/>\n            console.log(e);<\/p>\n<p>            setErr(e)<br \/>\n        } finally {<br \/>\n            setLoading(false)<br \/>\n        }<br \/>\n    })<br \/>\n    const updataData &#061; (async (newStu, id) &#061;&gt; {<br \/>\n        try {<br \/>\n            setErr(null)<br \/>\n            setLoading(true)<br \/>\n            const res &#061; await fetch(http:\/\/localhost:1337\/api\/students\/${id}, {<br \/>\n                method: &#039;PUT&#039;,<br \/>\n                body: JSON.stringify({ data: newStu }),<br \/>\n                headers: {<br \/>\n                    &#039;Content-Type&#039;: &#039;application\/json&#039;<br \/>\n                }<br \/>\n            })<br \/>\n            if (!res.ok) {<br \/>\n                throw new Error(&#039;\u4fee\u6539\u51fa\u9519&#039;)<br \/>\n            }<br \/>\n            Stx.fetchData()<br \/>\n        } catch (e) {<br \/>\n            setErr(e)<br \/>\n        } finally {<br \/>\n            setLoading(false)<br \/>\n        }<br \/>\n    })<br \/>\n    const nameChange &#061; (e) &#061;&gt; {<br \/>\n        setInput((preState) &#061;&gt; ({ &#8230;preState, name: e.target.value }))<br \/>\n    }<br \/>\n    const ageChange &#061; (e) &#061;&gt; {<br \/>\n        setInput((preState) &#061;&gt; ({ &#8230;preState, age: &#043;e.target.value }))<br \/>\n    }<br \/>\n    const genderChange &#061; (e) &#061;&gt; {<br \/>\n        setInput((preState) &#061;&gt; ({ &#8230;preState, gender: e.target.value }))<br \/>\n    }<br \/>\n    const addressChange &#061; (e) &#061;&gt; {<br \/>\n        setInput((preState) &#061;&gt; ({ &#8230;preState, address: e.target.value }))<br \/>\n    }<br \/>\n    const handle &#061; () &#061;&gt; {<br \/>\n        addData(input)<br \/>\n    }<br \/>\n    const updataHandle &#061; () &#061;&gt; {<br \/>\n        updataData(input, props.stu.documentId)<br \/>\n    }<br \/>\n    return (<\/p>\n<p>        &lt;&gt;<\/p>\n<p>            &lt;tr className&#061;&#039;student-form&#039;&gt;<br \/>\n                &lt;td&gt;&lt;input type&#061;&#034;text&#034; onChange&#061;{nameChange} value&#061;{input.name} \/&gt;&lt;\/td&gt;<br \/>\n                &lt;td&gt;&lt;select name&#061;&#034;&#034; id&#061;&#034;&#034; onChange&#061;{genderChange} value&#061;{input.gender}&gt;<br \/>\n                    &lt;option value&#061;&#034;\u7537&#034;&gt;\u7537&lt;\/option&gt;<br \/>\n                    &lt;option value&#061;&#034;\u5973&#034;&gt;\u5973&lt;\/option&gt;<br \/>\n                &lt;\/select&gt;&lt;\/td&gt;<br \/>\n                &lt;td&gt;&lt;input type&#061;&#034;text&#034; onChange&#061;{ageChange} value&#061;{input.age} \/&gt;&lt;\/td&gt;<br \/>\n                &lt;td&gt;&lt;input type&#061;&#034;text&#034; onChange&#061;{addressChange} value&#061;{input.address} \/&gt;&lt;\/td&gt;<\/p>\n<p>                &lt;td&gt;<br \/>\n                    {props.stu &amp;&amp; &lt;&gt;&lt;button onClick&#061;{() &#061;&gt; { props.onCancel() }}&gt;\u53d6\u6d88&lt;\/button&gt;&lt;button onClick&#061;{updataHandle}&gt;\u4fee\u6539&lt;\/button&gt;&lt;\/&gt;}<br \/>\n                    {!props.stu &amp;&amp; &lt;button onClick&#061;{handle}&gt;\u6dfb\u52a0&lt;\/button&gt;}<\/p>\n<p>                &lt;\/td&gt;<br \/>\n            &lt;\/tr&gt;<br \/>\n            {loading &amp;&amp; &lt;tr&gt;&lt;td colSpan&#061;{5}&gt;\u6dfb\u52a0\u4e2d&lt;\/td&gt;&lt;\/tr&gt;}<br \/>\n            {err &amp;&amp; &lt;tr&gt;&lt;td colSpan&#061;{5}&gt;\u6dfb\u52a0\u5931\u8d25&lt;\/td&gt;&lt;\/tr&gt;}<br \/>\n        &lt;\/&gt;<br \/>\n    )<br \/>\n}<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u552f\u4e00\u4e0d\u540c\u7684\u662f\u6211\u4eec\u5728\u6267\u884c\u521b\u5efa\u6216\u8005\u66f4\u65b0\u8bf7\u6c42\u9664\u4e86\u8bf7\u6c42\u5934\u4e0d\u540c\u4ee5\u5916&#xff0c;\u521b\u5efa\u4e5f\u5c31\u662f\u6dfb\u52a0\u65b0\u8868\u5143\u7d20\u9700\u8981\u4f20\u4e00\u4e2ajson\u683c\u5f0f\u7684\u6587\u672c\u5185\u5bb9&#xff0c;\u4e5f\u5c31\u662f\u6211\u4eec\u9700\u8981\u628ajs\u5bf9\u8c61\u8f6c\u5316\u6210json\u683c\u5f0f\u3002\u6b63\u597d\u5bf9\u5e94\u6211\u4eec\u54cd\u5e94\u4f53json\u683c\u5f0f\u9700\u8981\u8f6c\u5316\u6210js\u5bf9\u8c61\u6211\u4eec\u7528\u3002\u4ee5\u53ca\u6307\u660e\u8bf7\u6c42\u4f53\u7684\u683c\u5f0f&#xff0c;headers&#xff1a;{\u201cContent-Type:&#039;application\/json&#039;\u201d}\u8bf4\u660e\u6211\u4eec\u4f20\u8fc7\u53bb\u7684\u662fjson\u683c\u5f0f\u3002\u5305\u62ec\u66f4\u65b0&#xff0c;\u6bd5\u7adf\u6211\u4eec\u662f\u4f20\u8fc7\u53bb\u6570\u636e\u800c\u4e0d\u662fget\u83b7\u53d6\u6570\u636e\u3002\u8fd9\u4e00\u70b9\u8981\u6ce8\u610f\u3002\u597d\u4e86\u73b0\u5728\u6211\u4eec\u7528fetch\u5b9e\u73b0\u4e86post \u589e delete\u5220 put \u6539 get\u67e5\u4e86\u3002\u867d\u7136\u4e0d\u719f\u7ec3\u4f46\u81f3\u5c11\u6709\u4e2a\u601d\u8def\u600e\u4e48\u53bb\u6570\u636e\u5e93\u62ff\u4e1c\u897f\u4e86\u3002\u81f3\u4e8e\u4f18\u5316\u8fd8\u662f\u6162\u6162\u6765\u5427\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/p>\n<p>\u00a0<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb900\u6b21\uff0c\u70b9\u8d5e34\u6b21\uff0c\u6536\u85cf31\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528Strapi\u6784\u5efaAPI\u7ba1\u7406\u7cfb\u7edf\u548cReact\u524d\u7aef\u901a\u8fc7fetch\u5b9e\u73b0CRUD\u64cd\u4f5c\u7684\u6280\u672f\u65b9\u6848\uff1a Strapi\u642d\u5efa\uff1a\u901a\u8fc7npx create-strapi\u5feb\u901f\u521b\u5efa\u53ef\u89c6\u5316API\u7ba1\u7406\u7cfb\u7edf\uff0c\u652f\u6301\u7f51\u9875\u7aef\u5b9a\u4e49\u6570\u636e\u6a21\u578b\u548c\u6743\u9650\u8bbe\u7f6e\uff1b \u73af\u5883\u914d\u7f6e\uff1a\u4f7f\u7528nvm\u7ba1\u7406Node.js\u7248\u672c\uff0c\u786e\u4fdd\u517c\u5bb9\u6027\uff1b fetch\u8bf7\u6c42\uff1a\u8be6\u7ec6\u6f14\u793a\u4e86GET\u8bf7\u6c42\u83b7\u53d6\u5b66\u751f\u6570\u636e\u7684\u8fc7\u7a0b\uff0c\u5305\u62ec\u54cd\u5e94\u5904\u7406\u3001\u9519\u8bef\u6355\u83b7\u548c\u72b6\u6001\u7ba1\u7406\uff1b \u5f02\u6b65\u4f18\u5316\uff1a\u5bf9\u6bd4.then\u94fe\u5f0f\u8c03\u7528\u548casync\/await\u8bed\u6cd5\u7cd6\u4e24\u79cd\u5f02\u6b65\u5904\u7406\u65b9\u5f0f\uff1b \u5b8c\u6574CRUD\uff1a\u5b9e\u73b0POST\u521b\u5efa\u3001DELETE\u5220\u9664\u3001PU_fetch \u540c\u6b65\u8bf7\u6c42<\/p>\n","protected":false},"author":2,"featured_media":45948,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[524,87,43,44],"topic":[],"class_list":["post-45958","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-react-js","tag-87","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u5feb\u901f\u642d\u5efa\u670d\u52a1\u5668\uff0cfetch\u8bf7\u6c42\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e - \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\/45958.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u5feb\u901f\u642d\u5efa\u670d\u52a1\u5668\uff0cfetch\u8bf7\u6c42\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb900\u6b21\uff0c\u70b9\u8d5e34\u6b21\uff0c\u6536\u85cf31\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528Strapi\u6784\u5efaAPI\u7ba1\u7406\u7cfb\u7edf\u548cReact\u524d\u7aef\u901a\u8fc7fetch\u5b9e\u73b0CRUD\u64cd\u4f5c\u7684\u6280\u672f\u65b9\u6848\uff1a Strapi\u642d\u5efa\uff1a\u901a\u8fc7npx create-strapi\u5feb\u901f\u521b\u5efa\u53ef\u89c6\u5316API\u7ba1\u7406\u7cfb\u7edf\uff0c\u652f\u6301\u7f51\u9875\u7aef\u5b9a\u4e49\u6570\u636e\u6a21\u578b\u548c\u6743\u9650\u8bbe\u7f6e\uff1b \u73af\u5883\u914d\u7f6e\uff1a\u4f7f\u7528nvm\u7ba1\u7406Node.js\u7248\u672c\uff0c\u786e\u4fdd\u517c\u5bb9\u6027\uff1b fetch\u8bf7\u6c42\uff1a\u8be6\u7ec6\u6f14\u793a\u4e86GET\u8bf7\u6c42\u83b7\u53d6\u5b66\u751f\u6570\u636e\u7684\u8fc7\u7a0b\uff0c\u5305\u62ec\u54cd\u5e94\u5904\u7406\u3001\u9519\u8bef\u6355\u83b7\u548c\u72b6\u6001\u7ba1\u7406\uff1b \u5f02\u6b65\u4f18\u5316\uff1a\u5bf9\u6bd4.then\u94fe\u5f0f\u8c03\u7528\u548casync\/await\u8bed\u6cd5\u7cd6\u4e24\u79cd\u5f02\u6b65\u5904\u7406\u65b9\u5f0f\uff1b \u5b8c\u6574CRUD\uff1a\u5b9e\u73b0POST\u521b\u5efa\u3001DELETE\u5220\u9664\u3001PU_fetch \u540c\u6b65\u8bf7\u6c42\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/45958.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-29T18:01:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729180112-68890c68a8caf.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=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/45958.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/45958.html\",\"name\":\"\u5feb\u901f\u642d\u5efa\u670d\u52a1\u5668\uff0cfetch\u8bf7\u6c42\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-29T18:01:15+00:00\",\"dateModified\":\"2025-07-29T18:01:15+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/45958.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/45958.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/45958.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u5feb\u901f\u642d\u5efa\u670d\u52a1\u5668\uff0cfetch\u8bf7\u6c42\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e\"}]},{\"@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":"\u5feb\u901f\u642d\u5efa\u670d\u52a1\u5668\uff0cfetch\u8bf7\u6c42\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e - \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\/45958.html","og_locale":"zh_CN","og_type":"article","og_title":"\u5feb\u901f\u642d\u5efa\u670d\u52a1\u5668\uff0cfetch\u8bf7\u6c42\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb900\u6b21\uff0c\u70b9\u8d5e34\u6b21\uff0c\u6536\u85cf31\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528Strapi\u6784\u5efaAPI\u7ba1\u7406\u7cfb\u7edf\u548cReact\u524d\u7aef\u901a\u8fc7fetch\u5b9e\u73b0CRUD\u64cd\u4f5c\u7684\u6280\u672f\u65b9\u6848\uff1a Strapi\u642d\u5efa\uff1a\u901a\u8fc7npx create-strapi\u5feb\u901f\u521b\u5efa\u53ef\u89c6\u5316API\u7ba1\u7406\u7cfb\u7edf\uff0c\u652f\u6301\u7f51\u9875\u7aef\u5b9a\u4e49\u6570\u636e\u6a21\u578b\u548c\u6743\u9650\u8bbe\u7f6e\uff1b \u73af\u5883\u914d\u7f6e\uff1a\u4f7f\u7528nvm\u7ba1\u7406Node.js\u7248\u672c\uff0c\u786e\u4fdd\u517c\u5bb9\u6027\uff1b fetch\u8bf7\u6c42\uff1a\u8be6\u7ec6\u6f14\u793a\u4e86GET\u8bf7\u6c42\u83b7\u53d6\u5b66\u751f\u6570\u636e\u7684\u8fc7\u7a0b\uff0c\u5305\u62ec\u54cd\u5e94\u5904\u7406\u3001\u9519\u8bef\u6355\u83b7\u548c\u72b6\u6001\u7ba1\u7406\uff1b \u5f02\u6b65\u4f18\u5316\uff1a\u5bf9\u6bd4.then\u94fe\u5f0f\u8c03\u7528\u548casync\/await\u8bed\u6cd5\u7cd6\u4e24\u79cd\u5f02\u6b65\u5904\u7406\u65b9\u5f0f\uff1b \u5b8c\u6574CRUD\uff1a\u5b9e\u73b0POST\u521b\u5efa\u3001DELETE\u5220\u9664\u3001PU_fetch \u540c\u6b65\u8bf7\u6c42","og_url":"https:\/\/www.wsisp.com\/helps\/45958.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-29T18:01:15+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729180112-68890c68a8caf.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/45958.html","url":"https:\/\/www.wsisp.com\/helps\/45958.html","name":"\u5feb\u901f\u642d\u5efa\u670d\u52a1\u5668\uff0cfetch\u8bf7\u6c42\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-29T18:01:15+00:00","dateModified":"2025-07-29T18:01:15+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/45958.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/45958.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/45958.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u5feb\u901f\u642d\u5efa\u670d\u52a1\u5668\uff0cfetch\u8bf7\u6c42\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e"}]},{"@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\/45958","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=45958"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/45958\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/45948"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=45958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=45958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=45958"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=45958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}