{"id":52874,"date":"2024-12-03T10:34:11","date_gmt":"2024-12-03T02:34:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52874\/"},"modified":"2024-12-03T10:34:11","modified_gmt":"2024-12-03T02:34:11","slug":"axios-%e4%b8%8e-fetch%ef%bc%9a%e5%93%aa%e4%b8%aa%e6%9c%80%e9%80%82%e5%90%88-http-%e8%af%b7%e6%b1%82%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52874\/","title":{"rendered":"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aAxios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p>\u5728 javascript \u4e2d\u53d1\u51fa http \u8bf7\u6c42\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f46\u6700\u6d41\u884c\u7684\u4e24\u79cd\u662f axios \u548c\u672c\u673a fetch() api\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u6bd4\u8f83\u548c\u5bf9\u6bd4\u8fd9\u4e24\u79cd\u65b9\u6cd5\uff0c\u4ee5\u786e\u5b9a\u54ea\u4e00\u79cd\u66f4\u9002\u5408\u4e0d\u540c\u7684\u573a\u666f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/17324226506742abfac0b56.jpg\" class=\"aligncenter\" title=\"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u63d2\u56fe\" alt=\"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u63d2\u56fe\" \/><\/p>\n<h2> http \u8bf7\u6c42\u7684\u57fa\u672c\u4f5c\u7528 <\/h2>\n<p>http \u8bf7\u6c42\u662f\u4e0e web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u670d\u52a1\u5668\u548c api \u8fdb\u884c\u901a\u4fe1\u7684\u57fa\u7840\u3002 axios \u548c fetch() \u90fd\u88ab\u5e7f\u6cdb\u7528\u4e8e\u6709\u6548\u5730\u4fc3\u8fdb\u8fd9\u4e9b\u8bf7\u6c42\u3002\u8ba9\u6211\u4eec\u6df1\u5165\u7814\u7a76\u5b83\u4eec\u7684\u529f\u80fd\uff0c\u770b\u770b\u5b83\u4eec\u5982\u4f55\u53e0\u52a0\u3002<\/p>\n<h2> \u4ec0\u4e48\u662f axios\uff1f <\/h2>\n<p><strong>axios<\/strong> \u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u5b83\u63d0\u4f9b\u57fa\u4e8e promise \u7684 http \u5ba2\u6237\u7aef\u6765\u53d1\u51fa http \u8bf7\u6c42\u3002\u5b83\u4ee5\u5176\u7b80\u5355\u6027\u548c\u7075\u6d3b\u6027\u800c\u95fb\u540d\uff0c\u5728 javascript \u793e\u533a\u4e2d\u5f97\u5230\u5e7f\u6cdb\u4f7f\u7528\u3002<\/p>\n<h3> axios\u57fa\u672c\u8bed\u6cd5 <\/h3>\n<pre>axios(config)\n  .then(response =&gt; console.log(response.data))\n  .catch(error =&gt; console.error('error:', error));\n<\/pre>\n<h3> axios \u7684\u4e3b\u8981\u7279\u70b9\uff1a <\/h3>\n<ul>\n<li> <strong>\u914d\u7f6e\u7075\u6d3b\u6027\uff1a<\/strong>\u540c\u65f6\u63a5\u53d7 url \u548c\u914d\u7f6e\u5bf9\u8c61\u3002<\/li>\n<li> <strong>\u81ea\u52a8\u6570\u636e\u5904\u7406\uff1a<\/strong> \u81ea\u52a8\u5c06\u6570\u636e\u4e0e json \u76f8\u4e92\u8f6c\u6362\u3002<\/li>\n<li> <strong>\u9519\u8bef\u5904\u7406\uff1a<\/strong>\u81ea\u52a8\u5904\u7406http\u9519\u8bef\u72b6\u6001\u4ee3\u7801\uff0c\u5c06\u5b83\u4eec\u4f20\u9012\u7ed9catch\u5757\u3002<\/li>\n<li> <strong>\u7b80\u5316\u54cd\u5e94\uff1a<\/strong>\u76f4\u63a5\u5728\u54cd\u5e94\u5bf9\u8c61\u7684 data \u5c5e\u6027\u4e2d\u8fd4\u56de\u670d\u52a1\u5668\u6570\u636e\u3002<\/li>\n<li> <strong>\u7b80\u5316\u7684\u9519\u8bef\u7ba1\u7406\uff1a<\/strong>\u63d0\u4f9b\u66f4\u7b80\u5316\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002<\/li>\n<\/ul>\n<h3> \u4f8b\u5b50\uff1a <\/h3>\n<pre>axios({\n  method: 'post',\n  url: 'https:\/\/api.example.com\/data',\n  data: { key: 'value' }\n})\n  .then(response =&gt; console.log(response.data))\n  .catch(error =&gt; {\n    if (error.response) {\n      console.error('server responded with:', error.response.status);\n    } else if (error.request) {\n      console.error('no response received');\n    } else {\n      console.error('error:', error.message);\n    }\n  });\n<\/pre>\n<h3> \u4e3a\u4ec0\u4e48\u4f7f\u7528 axios\uff1f <\/h3>\n<ul>\n<li> <strong>\u81ea\u52a8 json \u6570\u636e\u8f6c\u6362\uff1a<\/strong> \u65e0\u7f1d\u5730\u5c06\u6570\u636e\u4e0e json \u76f8\u4e92\u8f6c\u6362\u3002<\/li>\n<li> <strong>\u54cd\u5e94\u8d85\u65f6\uff1a<\/strong>\u5141\u8bb8\u8bbe\u7f6e\u8bf7\u6c42\u8d85\u65f6\u3002<\/li>\n<li> <strong>http \u62e6\u622a\u5668\uff1a<\/strong> \u8ba9\u60a8\u62e6\u622a\u8bf7\u6c42\u548c\u54cd\u5e94\u3002<\/li>\n<li> <strong>\u4e0b\u8f7d\u8fdb\u5ea6\uff1a<\/strong>\u8ddf\u8e2a\u4e0b\u8f7d\u548c\u4e0a\u4f20\u7684\u8fdb\u5ea6\u3002<\/li>\n<li> <strong>\u540c\u65f6\u8bf7\u6c42\uff1a<\/strong>\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\u5e76\u5408\u5e76\u54cd\u5e94\u3002<\/li>\n<\/ul>\n<h2> \u4ec0\u4e48\u662f\u6293\u53d6\uff1f <\/h2>\n<p><strong>fetch()<\/strong> \u662f\u73b0\u4ee3 javascript \u4e2d\u7684\u5185\u7f6e api\uff0c\u6240\u6709\u73b0\u4ee3\u6d4f\u89c8\u5668\u90fd\u652f\u6301\u3002\u5b83\u662f\u4e00\u4e2a\u5f02\u6b65 web api\uff0c\u4ee5 promise \u7684\u5f62\u5f0f\u8fd4\u56de\u6570\u636e\u3002<\/p>\n<h3> fetch() \u7684\u7279\u70b9\uff1a <\/h3>\n<ul>\n<li> <strong>\u57fa\u672c\u8bed\u6cd5\uff1a<\/strong> \u7b80\u5355\u660e\u4e86\uff0c\u91c7\u7528 url \u548c\u53ef\u9009\u9009\u9879\u5bf9\u8c61\u3002<\/li>\n<li> <strong>\u5411\u540e\u517c\u5bb9\u6027\uff1a<\/strong>\u53ef\u4ee5\u5728\u5e26\u6709polyfills\u7684\u65e7\u7248\u6d4f\u89c8\u5668\u4e2d\u4f7f\u7528\u3002<\/li>\n<li> <strong>\u53ef\u5b9a\u5236\uff1a<\/strong>\u5141\u8bb8\u5bf9\u6807\u5934\u3001\u6b63\u6587\u3001\u65b9\u6cd5\u3001\u6a21\u5f0f\u3001\u51ed\u636e\u3001\u7f13\u5b58\u3001\u91cd\u5b9a\u5411\u548c\u5f15\u7528\u7b56\u7565\u8fdb\u884c\u8be6\u7ec6\u63a7\u5236\u3002<\/li>\n<\/ul>\n<h3> \u5982\u4f55\u4f7f\u7528axios\u53d1\u51fahttp\u8bf7\u6c42 <\/h3>\n<p>\u9996\u5148\uff0c\u4f7f\u7528npm\u6216yarn\u5b89\u88c5axios\uff1a<\/p>\n<pre>npm install axios\n# or\nyarn add axios\n# or\npnpm install axios\n<\/pre>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7 cdn \u5305\u542b axios\uff1a<\/p>\n<pre>&lt;script src=\"https:\/\/unpkg.com\/axios\/dist\/axios.min.js\"&gt;&lt;\/script&gt;\n<\/pre>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 axios \u53d1\u51fa get \u8bf7\u6c42\uff1a<\/p>\n<pre>import axios from 'axios';\n\naxios.get('https:\/\/example.com\/api')\n  .then(response =&gt; console.log(response.data))\n  .catch(error =&gt; console.error(error));\n<\/pre>\n<h3> \u4f7f\u7528 fetch \u53d1\u51fa http \u8bf7\u6c42 <\/h3>\n<p>\u7531\u4e8e fetch() \u662f\u5185\u7f6e\u7684\uff0c\u56e0\u6b64\u60a8\u4e0d\u9700\u8981\u5b89\u88c5\u4efb\u4f55\u4e1c\u897f\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 fetch() \u53d1\u51fa get \u8bf7\u6c42\uff1a<\/p>\n<pre>fetch('https:\/\/example.com\/api')\n  .then(response =&gt; response.json())\n  .then(data =&gt; console.log(data))\n  .catch(error =&gt; console.error(error));\n<\/pre>\n<h4> \u8bf7\u6ce8\u610f\uff1a <\/h4>\n<ul>\n<li> <strong>\u6570\u636e\u5904\u7406\uff1a<\/strong> axios \u81ea\u52a8\u5c06\u6570\u636e\u4e0e json \u76f8\u4e92\u8f6c\u6362\uff0c\u800c\u4f7f\u7528 fetch() \u65f6\uff0c\u60a8\u5fc5\u987b\u624b\u52a8\u8c03\u7528 response.json()\u3002<\/li>\n<li> <strong>\u9519\u8bef\u5904\u7406\uff1a<\/strong> axios \u5904\u7406 catch \u5757\u5185\u7684\u9519\u8bef\uff0c\u800c fetch() \u4ec5\u62d2\u7edd\u7f51\u7edc\u9519\u8bef\u7684\u627f\u8bfa\uff0c\u800c\u4e0d\u62d2\u7edd http \u72b6\u6001\u9519\u8bef\u3002<\/li>\n<\/ul>\n<h3> fetch \u7684\u57fa\u672c\u8bed\u6cd5 <\/h3>\n<pre>fetch(url, options)\n  .then(response =&gt; response.json())\n  .then(data =&gt; console.log(data))\n  .catch(error =&gt; console.error('error:', error));\n<\/pre>\n<h3> \u4e3b\u8981\u7279\u70b9\uff1a <\/h3>\n<ul>\n<li> <strong>\u7b80\u5355\u53c2\u6570\uff1a<\/strong> \u91c7\u7528 url \u548c\u53ef\u9009\u914d\u7f6e\u5bf9\u8c61\u3002<\/li>\n<li> <strong>\u624b\u52a8\u6570\u636e\u5904\u7406\uff1a<\/strong>\u9700\u8981\u624b\u52a8\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/li>\n<li> <strong>\u54cd\u5e94\u5bf9\u8c61\uff1a<\/strong>\u8fd4\u56de\u5305\u542b\u5b8c\u6574\u54cd\u5e94\u4fe1\u606f\u7684\u54cd\u5e94\u5bf9\u8c61\u3002<\/li>\n<li> <strong>\u9519\u8bef\u5904\u7406\uff1a<\/strong>\u9700\u8981\u624b\u52a8\u68c0\u67e5 http \u9519\u8bef\u7684\u54cd\u5e94\u72b6\u6001\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<h3> \u4f8b\u5b50\uff1a <\/h3>\n<pre>fetch('https:\/\/api.example.com\/data', {\n  method: 'post',\n  headers: { 'content-type': 'application\/json' },\n  body: json.stringify({ key: 'value' })\n})\n  .then(response =&gt; {\n    if (!response.ok) throw new error('http error ' + response.status);\n    return response.json();\n  })\n  .then(data =&gt; console.log(data))\n  .catch(error =&gt; console.error('error:', error));\n<\/pre>\n<h2> axios \u4e0e fetch \u7684\u6bd4\u8f83 <\/h2>\n<h3> \u53d1\u9001\u5e26\u6709\u67e5\u8be2\u53c2\u6570\u7684 get \u8bf7\u6c42 <\/h3>\n<p><em>axios\uff1a<\/em><\/p>\n<pre>axios.get('\/api\/data', { params: { name: 'alice', age: 25 } })\n  .then(response =&gt; { \/* handle response *\/ })\n  .catch(error =&gt; { \/* handle error *\/ });\n<\/pre>\n<p><em>\u83b7\u53d6\uff1a<\/em><\/p>\n<pre>const url = new url('\/api\/data');\nurl.searchparams.append('name', 'alice');\nurl.searchparams.append('age', 25);\n\nfetch(url)\n  .then(response =&gt; response.json())\n  .then(data =&gt; { \/* handle data *\/ })\n  .catch(error =&gt; { \/* handle error *\/ });\n<\/pre>\n<h3> \u4f7f\u7528 json \u6b63\u6587\u53d1\u9001 post \u8bf7\u6c42 <\/h3>\n<p><em>axios\uff1a<\/em><\/p>\n<pre>axios.post('\/api\/data', { name: 'bob', age: 30 })\n  .then(response =&gt; { \/* handle response *\/ })\n  .catch(error =&gt; { \/* handle error *\/ });\n<\/pre>\n<p><em>\u83b7\u53d6\uff1a<\/em><\/p>\n<pre>fetch('\/api\/data', {\n  method: 'post',\n  headers: { 'content-type': 'application\/json' },\n  body: json.stringify({ name: 'bob', age: 30 })\n})\n  .then(response =&gt; response.json())\n  .then(data =&gt; { \/* handle data *\/ })\n  .catch(error =&gt; { \/* handle error *\/ });\n<\/pre>\n<h3> \u8bbe\u7f6e\u8bf7\u6c42\u8d85\u65f6 <\/h3>\n<p><em>axios\uff1a<\/em><\/p>\n<pre>axios.get('\/api\/data', { timeout: 5000 }) \/\/ 5 seconds\n  .then(response =&gt; { \/* handle response *\/ })\n  .catch(error =&gt; { \/* handle error *\/ });\n<\/pre>\n<p><em>\u83b7\u53d6\uff1a<\/em><\/p>\n<pre>const controller = new abortcontroller();\nconst signal = controller.signal;\n\nsettimeout(() =&gt; controller.abort(), 5000); \/\/ abort after 5 seconds\n\nfetch('\/api\/data', { signal })\n  .then(response =&gt; response.json())\n  .then(data =&gt; { \/* handle data *\/ })\n  .catch(error =&gt; { \/* handle error *\/ });\n<\/pre>\n<h3> \u4f7f\u7528 async\/await \u8bed\u6cd5 <\/h3>\n<p><em>axios\uff1a<\/em><\/p>\n<pre>async function getdata() {\n  try {\n    const response = await axios.get('\/api\/data');\n    \/\/ handle response\n  } catch (error) {\n    \/\/ handle error\n  }\n}\n<\/pre>\n<p><em>\u83b7\u53d6\uff1a<\/em><\/p>\n<pre>async function getdata() {\n  try {\n    const response = await fetch('\/api\/data');\n    const data = await response.json();\n    \/\/ handle data\n  } catch (error) {\n    \/\/ handle error\n  }\n}\n<\/pre>\n<h3> \u5411\u540e\u517c\u5bb9\u6027 <\/h3>\n<p><em>axios\uff1a<\/em><\/p>\n<ul>\n<li>\u9700\u8981\u5b89\u88c5\u5e76\u5305\u542b\u5728\u60a8\u7684\u9879\u76ee\u4e2d\u3002<\/li>\n<li>\u901a\u8fc7 promise \u548c\u5176\u4ed6\u73b0\u4ee3 javascript \u529f\u80fd\u7684 polyfills \u652f\u6301\u65e7\u7248\u6d4f\u89c8\u5668\u3002<\/li>\n<li>\u79ef\u6781\u7ef4\u62a4\u4e0e\u65b0\u73af\u5883\u7684\u517c\u5bb9\u6027\u3002<\/li>\n<\/ul>\n<p><em>\u83b7\u53d6\uff1a<\/em><\/p>\n<ul>\n<li>\u73b0\u4ee3\u6d4f\u89c8\u5668\u7684\u672c\u673a\u652f\u6301\u3002<\/li>\n<li>\u53ef\u4ee5\u8fdb\u884c\u586b\u5145\u4ee5\u652f\u6301\u65e7\u7248\u6d4f\u89c8\u5668\u3002<\/li>\n<li>\u7531\u6d4f\u89c8\u5668\u4f9b\u5e94\u5546\u81ea\u52a8\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<h3> \u9519\u8bef\u5904\u7406 <\/h3>\n<p><em>axios\uff1a<\/em><\/p>\n<p>\u5904\u7406 catch \u5757\u4e2d\u7684\u9519\u8bef\uff0c\u5e76\u5c06 2xx \u4e4b\u5916\u7684\u4efb\u4f55\u72b6\u6001\u4ee3\u7801\u89c6\u4e3a\u9519\u8bef\uff1a<\/p>\n<pre>try {\n  const response = await axios.get('\/api\/data');\n  \/\/ handle response\n} catch (error) {\n  console.log(error.response.data);\n}\n<\/pre>\n<p><em>\u83b7\u53d6\uff1a<\/em><\/p>\n<p>\u9700\u8981\u624b\u52a8\u72b6\u6001\u68c0\u67e5\uff1a<\/p>\n<pre>try {\n  const response = await fetch('\/api\/data');\n  if (!response.ok) throw new Error('HTTP error ' + response.status);\n  const data = await response.json();\n  \/\/ handle data\n} catch (error) {\n  console.log(error.message);\n}\n<\/pre>\n<h3> axios \u4e0e fetch\uff1a\u54ea\u4e2a\u6700\u597d\uff1f <\/h3>\n<p>\u6ca1\u6709\u660e\u786e\u7684\u7b54\u6848\uff0c\u8fd9\u53d6\u51b3\u4e8e\u60a8\u7684\u8981\u6c42\uff1a<\/p>\n<ul>\n<li> <strong>\u5982\u679c\u60a8\u9700\u8981\u81ea\u52a8 json \u6570\u636e\u8f6c\u6362\u3001http \u62e6\u622a\u5668\u548c\u9ad8\u7ea7\u9519\u8bef\u5904\u7406\u7b49\u529f\u80fd\uff0c\u8bf7\u4f7f\u7528 axios<\/strong>\u3002<\/li>\n<li> <strong>\u5982\u679c\u60a8\u60f3\u8981\u4e00\u4e2a\u5177\u6709\u5e7f\u6cdb\u81ea\u5b9a\u4e49\u9009\u9879\u7684\u672c\u673a\u3001\u8f7b\u91cf\u7ea7\u89e3\u51b3\u65b9\u6848\uff0c\u8bf7\u4f7f\u7528 fetch()<\/strong>\u3002<\/li>\n<\/ul>\n<h2> \u4f7f\u7528 echoapi \u751f\u6210 axios\/fetch \u4ee3\u7801 <\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/17324226506742abfac37fb.jpg\" class=\"aligncenter\" title=\"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u63d2\u56fe1\" alt=\"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u63d2\u56fe1\" \/><\/p>\n<p>echoapi \u662f\u4e00\u4e2a\u4e00\u4f53\u5316\u534f\u4f5c api \u5f00\u53d1\u5e73\u53f0\uff0c\u63d0\u4f9b\u7528\u4e8e\u8bbe\u8ba1\u3001\u8c03\u8bd5\u3001\u6d4b\u8bd5\u548c\u6a21\u62df api \u7684\u5de5\u5177\u3002 echoapi \u53ef\u4ee5\u81ea\u52a8\u751f\u6210\u7528\u4e8e\u53d1\u51fa http \u8bf7\u6c42\u7684 axios \u4ee3\u7801\u3002<\/p>\n<h3> \u4f7f\u7528 echoapi \u751f\u6210 axios \u4ee3\u7801\u7684\u6b65\u9aa4\uff1a <\/h3>\n<h4> 1. \u6253\u5f00echoapi\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u8bf7\u6c42\u3002 <\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/17324226516742abfbc50ce.jpg\" style=\"max-width:100%\" class=\"aligncenter\" title=\"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u63d2\u56fe2\" alt=\"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u63d2\u56fe2\" \/><\/p>\n<h4> 2. \u8f93\u5165api\u7aef\u70b9\u3001\u6807\u5934\u548c\u53c2\u6570\uff0c\u7136\u540e\u5355\u51fb\u201c\u4ee3\u7801\u7247\u6bb5\u201d\u3002 <\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/17324226526742abfc9baf0.jpg\" class=\"aligncenter\" title=\"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u63d2\u56fe3\" alt=\"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u63d2\u56fe3\" \/><\/p>\n<h4> 3. \u9009\u62e9\u201c\u751f\u6210\u5ba2\u6237\u7aef\u4ee3\u7801\u201d\u3002 <\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/17324226526742abfca0290.jpg\" class=\"aligncenter\" title=\"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u63d2\u56fe4\" alt=\"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u63d2\u56fe4\" \/><\/p>\n<h4> 4. \u5c06\u751f\u6210\u7684 axios \u4ee3\u7801\u590d\u5236\u5e76\u7c98\u8d34\u5230\u60a8\u7684\u9879\u76ee\u4e2d\u3002 <\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/17324226526742abfca52d1.jpg\" class=\"aligncenter\" title=\"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u63d2\u56fe5\" alt=\"Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u63d2\u56fe5\" \/><\/p>\n<h2> \u7ed3\u8bba <\/h2>\n<p>axios \u548c fetch() \u90fd\u662f\u5728 javascript \u4e2d\u53d1\u51fa http \u8bf7\u6c42\u7684\u5f3a\u5927\u65b9\u6cd5\u3002\u9009\u62e9\u6700\u9002\u5408\u60a8\u7684\u9879\u76ee\u9700\u6c42\u548c\u504f\u597d\u7684\u4e00\u79cd\u3002\u4f7f\u7528 echoapi \u7b49\u5de5\u5177\u53ef\u4ee5\u589e\u5f3a\u60a8\u7684\u5f00\u53d1\u5de5\u4f5c\u6d41\u7a0b\uff0c\u786e\u4fdd\u60a8\u7684\u4ee3\u7801\u51c6\u786e\u9ad8\u6548\u3002\u5feb\u4e50\u7f16\u7801\uff01<\/p>\n<p><\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aAxios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u4f7f\u7528 JavaScript \u5b9e\u73b0 CSS sticky \u6548\u679c\uff0c\u5373\u4f7f\u5143\u7d20\u9ad8\u5ea6\u8d85\u8fc7\u6d4f\u89c8\u5668\u7a97\u53e3\u9ad8\u5ea6\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Axios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f \u6536\u85cf \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aAxios \u4e0e Fetch\uff1a\u54ea\u4e2a\u6700\u9002\u5408 HTTP \u8bf7\u6c42\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ \u5728 javascript \u4e2d\u53d1\u51fa http \u8bf7\u6c42\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f46\u6700\u6d41\u884c\u7684\u4e24\u79cd\u662f axios \u548c\u672c\u673a fetch() api\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u6bd4\u8f83\u548c\u5bf9\u6bd4\u8fd9\u4e24\u79cd\u65b9\u6cd5\uff0c\u4ee5\u786e\u5b9a\u54ea\u4e00\u79cd\u66f4\u9002\u5408\u4e0d\u540c\u7684\u573a\u666f\u3002 http \u8bf7\u6c42\u7684\u57fa\u672c\u4f5c\u7528 http \u8bf7\u6c42\u662f\u4e0e web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u670d\u52a1\u5668\u548c api \u8fdb\u884c\u901a\u4fe1\u7684\u57fa\u7840\u3002 axios \u548c fetch() \u90fd\u88ab\u5e7f\u6cdb\u7528\u4e8e\u6709\u6548\u5730\u4fc3\u8fdb\u8fd9\u4e9b\u8bf7\u6c42\u3002\u8ba9\u6211\u4eec\u6df1\u5165\u7814\u7a76\u5b83\u4eec\u7684\u529f\u80fd\uff0c\u770b\u770b\u5b83\u4eec\u5982\u4f55\u53e0\u52a0\u3002 \u4ec0\u4e48\u662f axios\uff1f axios \u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u5b83\u63d0\u4f9b\u57fa\u4e8e promise \u7684 http \u5ba2\u6237\u7aef\u6765\u53d1\u51fa http \u8bf7\u6c42\u3002\u5b83\u4ee5\u5176\u7b80\u5355\u6027\u548c\u7075\u6d3b\u6027\u800c\u95fb\u540d\uff0c\u5728 javascript \u793e\u533a\u4e2d\u5f97\u5230\u5e7f\u6cdb\u4f7f\u7528\u3002 axios\u57fa\u672c\u8bed\u6cd5 axios(config) .then(response =&gt; console.log(response.data)) .catch(error =&gt; console.error(&#8216;error:&#8217;, error)); axios \u7684\u4e3b\u8981\u7279\u70b9\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52874","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52874"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52874\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}