{"id":3832,"date":"2021-08-24T21:42:12","date_gmt":"2021-08-24T13:42:12","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3832"},"modified":"2021-08-24T21:42:12","modified_gmt":"2021-08-24T13:42:12","slug":"axios%e7%9a%84%e4%b8%80%e4%ba%9b%e5%b8%b8%e8%a7%81%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3832","title":{"rendered":"axios\u7684\u4e00\u4e9b\u5e38\u89c1\u7528\u6cd5"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><a target=\"_blank\" href=\"https:\/\/link.juejin.im\/?target=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Faxios\" rel=\"noreferrer noopener\">axios<\/a>&nbsp;\u662f\u4e00\u4e2a\u5728\u524d\u7aef\u5f00\u53d1\u4e2d\u662f\u4f7f\u7528\u9891\u7387\u6781\u9ad8\u7684\u5e93\u3002\u5b83\u65e2\u80fd\u8fd0\u884c\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c\u53c8\u53ef\u5728 Node.js \u540e\u7aef\u9879\u76ee\u4e2d\u4f7f\u7528\u3002\u672c\u6587\u4e3b\u8981\u7ed3\u5408\u81ea\u5df1\u7684\u5f00\u53d1\u9879\u76ee\uff0c\u8c08\u4e00\u4e0b axios \u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u4e00\u4e9b\u5e38\u89c1\u7528\u6cd5\u3002<\/p><\/blockquote>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/h5>\n\n\n\n<p>\u5728\u9879\u76ee\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u00a0<code>axios.create<\/code>\u00a0\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5bf9\u8be5\u5b9e\u4f8b\u8fdb\u884c\u4e00\u4e9b\u914d\u7f6e\uff0c\u4fbf\u5f97\u5230\u4e86\u4e00\u4e2a\u4e13\u95e8\u7528\u6765\u4e0e\u540e\u7aef\u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1\u7684 ajax \u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const instance = axios.create()\n<\/pre>\n\n\n\n<p>\u4e0b\u9762\u4e00\u4e9b\u5bf9\u8be5\u5b9e\u4f8b\u7684\u914d\u7f6e\uff1a<\/p>\n\n\n\n<ul><li><strong>baseURL<\/strong><\/li><\/ul>\n\n\n\n<p>\u5728\u6b63\u5f0f\u73af\u5883\u6216\u8005\u662f\u5f00\u53d1\u73af\u5883\uff0c\u914d\u7f6e&nbsp;<code>baseURL<\/code>&nbsp;\u53ef\u4ee5\u8ba9\u6211\u4eec\u5f88\u65b9\u4fbf\u7684\u8bbe\u7f6e\u8def\u7531\u7684\u8f6c\u53d1\u89c4\u5219\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const instance = axios.create({\n    baseURL: '\/api\/'\n})<\/pre>\n\n\n\n<ul><li><strong>header<\/strong><\/li><\/ul>\n\n\n\n<p>\u5728\u5355\u7f51\u9875\u5e94\u7528\u4e2d\uff0c\u901a\u8fc7\u5728\u8bf7\u6c42\u5934\u8bbe\u7f6e\u4e00\u4e9b\u4fe1\u606f\uff0c\u6bd4\u5982\u8bf4\u628a token \u653e\u5728\u8bf7\u6c42\u7684\u5934\u90e8\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u7ee7\u7eed\u5bf9\u521a\u521a\u521b\u5efa\u7684\u793a\u4f8b\u8fdb\u884c\u914d\u7f6e\ninstance.defaults.headers.common['x-auth-token'] = 'some token'\n<\/pre>\n\n\n\n<ul><li><strong>interceptors<\/strong><\/li><\/ul>\n\n\n\n<p>\u901a\u8fc7\u914d\u7f6e\u62e6\u622a\u5668\uff0c\u5bf9\u8bf7\u6c42\u4e0e\u54cd\u5e94\u8fdb\u884c\u5904\u7406\u3002<br>\u5047\u8bbe\u8fd9\u91cc\u670d\u52a1\u5668\u7684\u8fd4\u56de\u503c\u7c7b\u4f3c\u8fd9\u79cd\u7ed3\u6784\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n    code: 200, \n    data: Object,\n    msg: ''\n}\n<\/pre>\n\n\n\n<p>\u5176\u4e2d data \u662f\u6211\u4eec\u771f\u6b63\u9700\u8981\u7684\u4fe1\u606f\uff0c\u6dfb\u52a0\u4e00\u4e2a response \u7684\u62e6\u622a\u5668\u5904\u7406\u63d0\u53d6\u5176\u4e2d\u7684 data \u3002\u901a\u8fc7\u68c0\u67e5 code \u503c\uff0c\u5224\u65ad\u54cd\u5e94\u503c\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002\u4e0d\u7b26\u5408\u8981\u6c42\u65f6\uff0c\u53ea\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\uff0c\u5728\u4e4b\u540e\u7684\u56de\u8c03\u51fd\u6570\u5904\u7406\u9519\u8bef\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u5904\u7406\u54cd\u5e94\u6210\u529f\n\/\/ axios \u4f1a\u5bf9\u54cd\u5e94\u7684\u7ed3\u679c\u8fdb\u884c\u4e00\u5c42\u5c01\u88c5\uff0c\u76f4\u63a5\u53d6data\ninstance.interceptors.response.use(({data}) => {\n    if (data.code === 200) {\n        return data.data\n    } else {\n        console.error(res)\n        \/\/ \u5047\u8bbe\u8fd9\u91cc\u6709\u4e00\u4e2a Toast \u7ec4\u4ef6\n        Toast(res.msg)\n        return Promise.reject({\n            msg: msg\n        })\n    }\n})\n<\/pre>\n\n\n\n<ul><li><strong>params \u7684\u7528\u6cd5<\/strong><\/li><\/ul>\n\n\n\n<p>\u6709\u4e9b\u65f6\u5019\uff0c\u8c03\u53d6\u4e00\u4e2a\u6709\u67e5\u8be2\u6761\u4ef6\u8f83\u591a\u7684\u63a5\u53e3\uff0c\u4f7f\u7528&nbsp;<code>axios.get<\/code>&nbsp;\u65f6\u4f20\u5165\u4e00\u4e2a\u5305\u542b&nbsp;<code>parmas<\/code>&nbsp;\u5b57\u6bb5\u7684\u5bf9\u8c61\u662f\u6700\u4fbf\u6377\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let params = {\n    keyWord: null,\n    name: 'xxx',\n    age: 22\n}\n\/\/ \u4e3a null \u7684\u5c5e\u6027\u90fd\u4f1a\u88ab\u8fc7\u6ee4\u6389\uff0c\u6700\u7ec8\u7684\u8bf7\u6c42 url \u662f \/person?name=xxx&amp;age=22\ninstance.get('\/person', { params })\n<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5 class=\"wp-block-heading\">\u4e0e\u72b6\u6001\u7ba1\u7406\u5e93\u914d\u5408\u4f7f\u7528<\/h5>\n\n\n\n<p>\u5728\u5355\u7f51\u9875\u5e94\u7528\u4e2d\uff0c\u7528\u6237\u7684 token \u4fe1\u606f\u662f\u4fdd\u5b58\u5728 store \u4e2d\u7684\u3002\u5f53\u7528\u6237\u767b\u5f55\u6216\u8005\u662f\u68c0\u6d4b\u5230token \u8fc7\u671f\u65f6\uff0c\u9700\u8981\u505a\u52a8\u6001\u6539\u53d8 axios \u5b9e\u4f8b\u7684\u4e00\u4e9b\u53c2\u6570\u3002<br>\u5047\u8bbe\u6211\u4eec\u8fd9\u91cc\u4f7f\u7528\u7684\u72b6\u6001\u7ba1\u7406\u5e93\u662f Redux\u3002<br>\u5728\u62e6\u622a\u5668\u4e2d\uff0c\u5224\u65ad token \u662f\u5426\u8fc7\u671f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">instance.interceptors.response.use(({data}) => {\n    if (data.code === 200) {\n        return data.data\n    } else if (data.code === 40001){ \/\/ code: 40001 \u8868\u793a token \u8fc7\u671f\n      store.commit(CLEAR_TOKEN)\n    }\n    ...\n})\n<\/pre>\n\n\n\n<p>\u5f53 store \u53d1\u751f\u6539\u53d8\u65f6\uff0c\u52a8\u6001\u6539\u53d8\u4f20\u9012\u7ed9\u540e\u7aef\u7684 token \u4fe1\u606f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">store.subscrible(() => {\n    instance.defaults.headers.common['token'] = store.getState().token\n})\n<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5 class=\"wp-block-heading\">vue\u5b8c\u6574\u7684\u62e6\u622aDemo<\/h5>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-69.png\" alt=\"\" class=\"wp-image-3835\" width=\"340\" height=\"107\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-69.png 496w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-69-300x94.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-69-230x72.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-69-350x110.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-69-480x151.png 480w\" sizes=\"(max-width: 340px) 100vw, 340px\" \/><\/figure><\/div>\n\n\n\n<p>AxiosConfig.js<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\nimport Axios from 'axios'\nimport Router from '@\/router'\nvar Constant = require('@\/common\/constant.js')\n\nconst axiosInstance = Axios.create({\n  baseURL: process.env.VUE_APP_API_URL,\n  timeout: Constant.API_TIME_OUT\n})\n\nvar msger = require('@\/common\/messager.js')\n\/\/ intercept the requst\naxiosInstance.interceptors.request.use(config => {\n  config.headers['apiKey'] = Constant.API_KEY\n  return config\n},\n error => {\n   msger.popMessage(error, 'error')\n   Promise.reject(error)\n })\n\n \/\/ intercept the response\naxiosInstance.interceptors.response.use(response => { \n    console.log(response.data)\n  if (response.data !== null &amp;&amp; response.data.status === 'ok') {\n    return response.data\n  } else {\n    var excpMsg = ''\n    if (response.data !== null) {\n      excpMsg = response.data.message\n    } else {\n      excpMsg = response\n    }\n    .................\n    }\n    return Promise.reject(response.data)\n  }\n}, \nerror => {  \n    if (error &amp;&amp; error.response) {\n      .................\n    } else {\n      let errorJson = JSON.parse(JSON.stringify(error))\n      msger.popMessage(errorJson.message + '(Network connection failed)', 'error')\n    }\n    return Promise.reject(error)\n})\n\nexport default axiosInstance<\/pre>\n\n\n\n<p>main.js<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import Vue from 'vue'\nimport App from '.\/App.vue'\nimport router from '.\/router'\nimport ElementUI from 'element-ui'\nimport locale from 'element-ui\/lib\/locale\/lang\/ja'\nimport 'element-ui\/lib\/theme-chalk\/index.css'\nimport store from '.\/store'\nimport axios from '.\/api\/AxiosConfig'\n\nVue.config.productionTip = false\nVue.prototype.$axios = axios\nvar msger = require('@\/common\/messager.js')\nVue.prototype.$msger = msger\nVue.use(ElementUI, { locale })\n\nnew Vue({\n  router,\n  store,\n  render: h => h(App),\n}).$mount('#app')\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>axios&nbsp;\u662f\u4e00\u4e2a\u5728\u524d\u7aef\u5f00\u53d1\u4e2d\u662f\u4f7f\u7528\u9891\u7387\u6781\u9ad8\u7684\u5e93\u3002\u5b83\u65e2\u80fd\u8fd0\u884c\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c\u53c8\u53ef\u5728 Node.js \u540e\u7aef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3832"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3832"}],"version-history":[{"count":5,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3832\/revisions"}],"predecessor-version":[{"id":3838,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3832\/revisions\/3838"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}