{"id":5887,"date":"2025-05-30T14:13:19","date_gmt":"2025-06-04T06:22:19","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5887"},"modified":"2025-06-04T14:13:19","modified_gmt":"2025-06-04T06:22:19","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%9c%a8vue-js%e4%b8%ad%e4%bd%bf%e7%94%a8axios%e9%80%b2%e8%a1%8chttp%e8%ab%8b%e6%b1%82","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/vue-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%9c%a8vue-js%e4%b8%ad%e4%bd%bf%e7%94%a8axios%e9%80%b2%e8%a1%8chttp%e8%ab%8b%e6%b1%82\/","title":{"rendered":"\u6df1\u5165\u63a2\u7d22\uff1a\u5982\u4f55\u5728 Vue.js \u4e2d\u9ad8\u6548\u4f7f\u7528 Axios \u9032\u884c HTTP \u8acb\u6c42"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Vue.js, axios, HTTP\u8acb\u6c42, \u524d\u7aef\u958b\u767c, JavaScript\"><\/p>\n<h1>\u6df1\u5165\u63a2\u7d22\uff1a\u5982\u4f55\u5728 Vue.js \u4e2d\u9ad8\u6548\u4f7f\u7528 Axios \u9032\u884c HTTP \u8acb\u6c42<\/h1>\n<p>Vue.js \u662f\u4e00\u500b\u6d41\u884c\u7684\u524d\u7aef\u6846\u67b6\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u69cb\u5efa\u9ad8\u6027\u80fd\u7684 Web \u61c9\u7528\u3002\u800c\u5728\u9019\u500b\u904e\u7a0b\u4e2d\uff0c\u9032\u884c HTTP \u8acb\u6c42\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u9019\u6642\u5019 Axios \u9019\u500b\u57fa\u65bc Promise \u7684 HTTP \u5ba2\u6236\u7aef\u5c31\u6210\u70ba\u4e86\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728 Vue.js \u4e2d\u904b\u7528 Axios \u4f86\u9032\u884c\u5404\u7a2e HTTP \u8acb\u6c42\uff0c\u4e26\u63d0\u4f9b\u5be6\u7528\u7684\u7bc4\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5b89\u88dd Axios<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd Axios\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 npm \u6216 yarn \u4f86\u8f15\u9b06\u5b89\u88dd\uff1a<\/p>\n<pre class=\"brush: python\">\nnpm install axios\n\nyarn add axios\n<\/pre>\n<h2>\u5728 Vue.js \u4e2d\u5c0e\u5165 Axios<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u4f60\u9700\u8981\u5728\u4f60\u7684 Vue.js \u61c9\u7528\u4e2d\u5c0e\u5165 Axios\uff0c\u4ee5\u4fbf\u80fd\u5920\u4f7f\u7528\u5176\u529f\u80fd\uff1a<\/p>\n<pre class=\"brush: python\">\nimport axios from 'axios';\n<\/pre>\n<h2>\u4f7f\u7528 Axios \u767c\u9001 GET \u8acb\u6c42<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Axios \u767c\u9001 GET \u8acb\u6c42\u3002\u4ee5\u4e0b\u7bc4\u4f8b\u5c07\u5411\u4f3a\u670d\u5668\u767c\u9001\u8acb\u6c42\u4e26\u986f\u793a\u56de\u61c9\uff1a<\/p>\n<pre class=\"brush: python\">\naxios.get('\/user?ID=12345')\n  .then((response) => {\n    console.log('\u6210\u529f\u7372\u53d6\u8cc7\u6599\uff1a', response.data);\n  })\n  .catch((error) => {\n    console.error('\u767c\u751f\u932f\u8aa4\uff1a', error);\n  });\n<\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u767c\u9001\u4e00\u500b GET \u8acb\u6c42\u81f3 `\/user?ID=12345`\uff0c\u4e26\u5728\u6210\u529f\u6642\u6253\u5370\u56de\u61c9\u8cc7\u6599\uff0c\u82e5\u51fa\u73fe\u932f\u8aa4\u5247\u986f\u793a\u932f\u8aa4\u8a0a\u606f\u3002<\/p>\n<h2>\u4f7f\u7528 Axios \u767c\u9001 POST \u8acb\u6c42<\/h2>\n<p>\u9664\u4e86 GET \u8acb\u6c42\uff0cAxios \u4e5f\u80fd\u8f15\u9b06\u767c\u9001 POST \u8acb\u6c42\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 Axios \u767c\u9001 POST \u8acb\u6c42\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\naxios.post('\/user', {\n  firstName: 'Fred',\n  lastName: 'Flintstone'\n})\n  .then((response) => {\n    console.log('\u8cc7\u6599\u6210\u529f\u767c\u9001\uff0c\u56de\u61c9\uff1a', response.data);\n  })\n  .catch((error) => {\n    console.error('\u767c\u751f\u932f\u8aa4\uff1a', error);\n  });\n<\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u767c\u9001\u4e00\u500b POST \u8acb\u6c42\u81f3 `\/user`\uff0c\u4e26\u5728\u6210\u529f\u6642\u986f\u793a\u56de\u61c9\u8cc7\u6599\uff0c\u82e5\u767c\u751f\u932f\u8aa4\u5247\u8f38\u51fa\u932f\u8aa4\u8a0a\u606f\u3002<\/p>\n<h2>\u6700\u4f73\u5be6\u8e10\u8207\u932f\u8aa4\u8655\u7406<\/h2>\n<p>\u5728\u9032\u884c HTTP \u8acb\u6c42\u6642\uff0c\u9069\u7576\u7684\u932f\u8aa4\u8655\u7406\u975e\u5e38\u91cd\u8981\u3002\u4f7f\u7528 Axios \u6642\uff0c\u53ef\u4ee5\u901a\u904e `.catch()` \u65b9\u6cd5\u6355\u6349\u548c\u8655\u7406\u932f\u8aa4\uff0c\u9019\u6a23\u53ef\u4ee5\u63d0\u5347\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u8207\u7528\u6236\u9ad4\u9a57\u3002\u6b64\u5916\uff0c\u5efa\u8b70\u5c07 Axios \u7684\u914d\u7f6e\u63d0\u53d6\u5230\u4e00\u500b\u55ae\u7368\u7684\u6a94\u6848\u4e2d\uff0c\u9019\u6a23\u53ef\u4ee5\u91cd\u8907\u4f7f\u7528\u4e26\u4fdd\u6301\u4ee3\u78bc\u6574\u6f54\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 Vue.js \u4e2d\u9ad8\u6548\u4f7f\u7528 Axios \u9032\u884c HTTP \u8acb\u6c42\uff0c\u4e26\u63d0\u4f9b\u4e86 GET \u548c POST \u8acb\u6c42\u7684\u7bc4\u4f8b\u3002\u900f\u904e\u638c\u63e1\u9019\u4e9b\u57fa\u790e\u77e5\u8b58\uff0c\u4f60\u5c07\u80fd\u5920\u66f4\u6709\u6548\u5730\u8207\u4f3a\u670d\u5668\u9032\u884c\u4ea4\u4e92\uff0c\u63d0\u5347\u4f60\u7684\u524d\u7aef\u958b\u767c\u6280\u80fd\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u66f4\u591a\u6709\u95dc Axios \u7684\u8cc7\u8a0a\uff0c\u53ef\u4ee5\u53c3\u8003\u4ee5\u4e0b\u8cc7\u6e90\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/axios\/axios\" target=\"_blank\" rel=\"noopener\">Axios Github \u5c08\u6848<\/a><\/li>\n<li><a href=\"https:\/\/www.npmjs.com\/package\/axios\" target=\"_blank\" rel=\"noopener\">Axios NPM \u5957\u4ef6<\/a><\/li>\n<\/ul>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Vue.js\u4e2d\u4f7f\u7528Axios\u9032\u884cHTTP\u8acb\u6c42\uff0cAxios\u662f\u4e00\u500b\u57fa\u65bcPromise\u7684HTTP\u5ba2\u6236\u7aef\uff0c\u53ef\u4ee5\u7528\u65bc\u767c\u9001\u8acb\u6c42\u548c\u63a5\u6536\u54cd\u61c9\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728Vue.js\u4e2d\u4f7f\u7528Axios\uff0c\u4ee5\u53ca\u5982\u4f55\u8655\u7406\u8acb\u6c42\u548c\u54cd\u61c9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[180,179],"tags":[178,177],"class_list":["post-5887","post","type-post","status-publish","format-standard","hentry","category-vue","category-vue-js","tag-vue","tag-vue-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1wX","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5887"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5887\/revisions"}],"predecessor-version":[{"id":5888,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5887\/revisions\/5888"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}