{"id":6021,"date":"2025-06-03T14:13:24","date_gmt":"2025-06-04T06:24:24","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6021"},"modified":"2025-06-04T14:13:24","modified_gmt":"2025-06-04T06:24:24","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8vue-js%e4%b8%ad%e7%9a%84axios%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%e4%bd%bf%e7%94%a8vue-js%e4%b8%ad%e7%9a%84axios%e9%80%b2%e8%a1%8chttp%e8%ab%8b%e6%b1%82\/","title":{"rendered":"\u5fb9\u5e95\u638c\u63e1 Vue.js \u4e2d Axios \u7684 HTTP \u8acb\u6c42\u6280\u5de7"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Vue.js, Axios, HTTP \u8acb\u6c42, JavaScript, \u524d\u7aef\u958b\u767c\"><\/p>\n<h1>\u5fb9\u5e95\u638c\u63e1 Vue.js \u4e2d Axios \u7684 HTTP \u8acb\u6c42\u6280\u5de7<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u524d\u7aef\u958b\u767c\u4e2d\uff0cVue.js \u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u7528\u6236\u7aef\u6846\u67b6\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u69cb\u5efa\u9ad8\u6548\u4e14\u4e92\u52d5\u6027\u5f37\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u800c Axios \u4f5c\u70ba\u57fa\u65bc Promise \u7684 HTTP \u5ba2\u6236\u7aef\uff0c\u4e0d\u50c5\u53ef\u4ee5\u5728\u700f\u89bd\u5668\u4e2d\u4f7f\u7528\uff0c\u9084\u80fd\u5728 Node.js \u74b0\u5883\u4e2d\u904b\u884c\u3002\u672c\u6587\u5c07\u5e36\u60a8\u6df1\u5165\u4e86\u89e3\u5982\u4f55\u5728 Vue.js \u4e2d\u9748\u6d3b\u904b\u7528 Axios \u767c\u9001 HTTP \u8acb\u6c42\uff0c\u8b93\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u66f4\u5177\u52d5\u614b\u6027\u3002<\/p>\n<h2>\u5982\u4f55\u5b89\u88dd Axios<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5c07 Axios \u5b89\u88dd\u5230\u60a8\u7684 Vue.js \u9805\u76ee\u4e2d\u3002\u60a8\u53ef\u4ee5\u9078\u64c7\u4f7f\u7528 npm \u6216 yarn \u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre class=\"brush: python\">\nnpm install axios\n\nyarn add axios\n<\/pre>\n<h2>\u5728 Vue.js \u4e2d\u4f7f\u7528 Axios \u767c\u9001 HTTP \u8acb\u6c42<\/h2>\n<p>\u5728\u60a8\u7684 Vue.js \u9805\u76ee\u4e2d\uff0c\u60a8\u53ea\u9700\u5728 `main.js` \u6587\u4ef6\u4e2d\u5f15\u5165 Axios\uff0c\u4e26\u5c07\u5176\u6dfb\u52a0\u5230 Vue \u7684\u539f\u578b\u4e2d\uff0c\u4ee5\u4fbf\u5728\u6574\u500b\u61c9\u7528\u4e2d\u8f15\u9b06\u8a2a\u554f\uff1a<\/p>\n<pre class=\"brush: python\">\nimport Vue from 'vue';\nimport axios from 'axios';\n\nVue.prototype.$axios = axios;\n<\/pre>\n<p>\u9019\u6a23\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u4efb\u4f55 Vue \u7d44\u4ef6\u4e2d\u4f7f\u7528 Axios \u767c\u9001 HTTP \u8acb\u6c42\u4e86\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u9019\u6a23\u7372\u53d6\u7528\u6236\u8cc7\u6599\uff1a<\/p>\n<pre class=\"brush: python\">\nexport default {\n  data() {\n    return {\n      users: []\n    };\n  },\n  created() {\n    this.fetchUsers();\n  },\n  methods: {\n    fetchUsers() {\n      this.$axios.get('\/api\/users')\n        .then(response => {\n          \/\/ \u8655\u7406\u6210\u529f\u7684\u56de\u61c9\n          this.users = response.data;\n          console.log(this.users);\n        })\n        .catch(error => {\n          \/\/ \u8655\u7406\u932f\u8aa4\n          console.error('Error fetching users:', error);\n        });\n    }\n  }\n};\n<\/pre>\n<h2>\u5c0f\u6280\u5de7\uff1a\u8655\u7406\u7570\u5e38\u60c5\u6cc1<\/h2>\n<p>\u7576\u9032\u884c HTTP \u8acb\u6c42\u6642\uff0c\u8655\u7406\u7570\u5e38\u60c5\u6cc1\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u60a8\u53ef\u4ee5\u5728 Axios \u4e2d\u8a2d\u7f6e\u5168\u5c40\u6514\u622a\u5668\u4f86\u6355\u7372\u932f\u8aa4\u4e26\u9032\u884c\u7d71\u4e00\u8655\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: python\">\naxios.interceptors.response.use(\n  response => response,\n  error => {\n    \/\/ \u5728\u9019\u88e1\u8655\u7406\u932f\u8aa4\n    console.error('HTTP Error:', error);\n    return Promise.reject(error);\n  }\n);\n<\/pre>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 Vue.js \u4e2d\u4f7f\u7528 Axios \u9032\u884c HTTP \u8acb\u6c42\u3002\u6211\u5011\u5b78\u6703\u4e86\u5982\u4f55\u5b89\u88dd Axios\uff0c\u5982\u4f55\u5728 Vue \u7d44\u4ef6\u4e2d\u4f7f\u7528\u5b83\u4f86\u767c\u9001\u8acb\u6c42\uff0c\u4ee5\u53ca\u5982\u4f55\u8655\u7406\u7570\u5e38\u60c5\u6cc1\u3002\u900f\u904e\u9019\u4e9b\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u7528\u6236\u9ad4\u9a57\uff0c\u8b93\u60a8\u7684\u958b\u767c\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p>\u7121\u8ad6\u60a8\u662f\u65b0\u624b\u9084\u662f\u6709\u7d93\u9a57\u7684\u958b\u767c\u8005\uff0c\u638c\u63e1 Axios \u9019\u4e00\u5de5\u5177\u90fd\u5c07\u70ba\u60a8\u7684 Vue.js \u958b\u767c\u5e36\u4f86\u5de8\u5927\u7684\u4fbf\u5229\u3002\u7acb\u5373\u958b\u59cb\u4f7f\u7528\u5427\uff0c\u8b93\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u66f4\u5177\u4e92\u52d5\u6027\u548c\u97ff\u61c9\u6027\uff01<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Vue.js\u4e2d\u7684Axios\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\u4f7f\u7528Axios\u9032\u884cHTTP\u8acb\u6c42\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-6021","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-1z7","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6021","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=6021"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6021\/revisions"}],"predecessor-version":[{"id":6022,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6021\/revisions\/6022"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}