{"id":3067,"date":"2018-08-30T16:56:46","date_gmt":"2018-08-30T08:56:46","guid":{"rendered":"http:\/\/www.daliane.com\/?p=3067"},"modified":"2018-08-30T16:56:46","modified_gmt":"2018-08-30T08:56:46","slug":"axios_jian_jie","status":"publish","type":"post","link":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/axios_jian_jie\/","title":{"rendered":"axios \u7b80\u4ecb"},"content":{"rendered":"<h2 id=\"axios-\u7b80\u4ecb\" style=\"font-size:21px;color:#333333;font-family:Verdana, Arial, Helvetica, sans-serif;background-color:#FFFFFF;\">\n\taxios \u7b80\u4ecb<br \/>\n<\/h2>\n<p style=\"color:#333333;font-family:Verdana, Arial, Helvetica, sans-serif;font-size:14px;background-color:#FFFFFF;\">\n\taxios \u662f\u4e00\u4e2a\u57fa\u4e8ePromise \u7528\u4e8e\u6d4f\u89c8\u5668\u548c nodejs \u7684 HTTP \u5ba2\u6237\u7aef\uff0c\u5b83\u672c\u8eab\u5177\u6709\u4ee5\u4e0b\u7279\u5f81\uff1a\n<\/p>\n<hr \/>\n<ul style=\"color:#333333;font-family:Verdana, Arial, Helvetica, sans-serif;font-size:14px;background-color:#FFFFFF;\">\n<li>\n\t\t\u4ece\u6d4f\u89c8\u5668\u4e2d\u521b\u5efa XMLHttpRequest\n\t<\/li>\n<li>\n\t\t\u4ece node.js \u53d1\u51fa http \u8bf7\u6c42\n\t<\/li>\n<li>\n\t\t\u652f\u6301 Promise API\n\t<\/li>\n<li>\n\t\t\u62e6\u622a\u8bf7\u6c42\u548c\u54cd\u5e94\n\t<\/li>\n<li>\n\t\t\u8f6c\u6362\u8bf7\u6c42\u548c\u54cd\u5e94\u6570\u636e\n\t<\/li>\n<li>\n\t\t\u53d6\u6d88\u8bf7\u6c42\n\t<\/li>\n<li>\n\t\t\u81ea\u52a8\u8f6c\u6362JSON\u6570\u636e\n\t<\/li>\n<li>\n\t\t\u5ba2\u6237\u7aef\u652f\u6301\u9632\u6b62&nbsp;CSRF\/XSRF\n\t<\/li>\n<\/ul>\n<p>\n\t\u6267\u884c GET \u8bf7\u6c42\n<\/p>\n<p>\n\t\n<\/p>\n<pre class=\"prettyprint lang-js\">\/\/ \u4e3a\u7ed9\u5b9a ID \u7684 user \u521b\u5efa\u8bf7\u6c42\r\naxios.get('\/user?ID=12345')\r\n  .then(function (response) {\r\n    console.log(response);\r\n  })\r\n  .catch(function (error) {\r\n    console.log(error);\r\n  });\r\n\r\n\/\/ \u53ef\u9009\u5730\uff0c\u4e0a\u9762\u7684\u8bf7\u6c42\u53ef\u4ee5\u8fd9\u6837\u505a\r\naxios.get('\/user', {\r\n    params: {\r\n      ID: 12345\r\n    }\r\n  })\r\n  .then(function (response) {\r\n    console.log(response);\r\n  })\r\n  .catch(function (error) {\r\n    console.log(error);\r\n  });<\/pre>\n<p>\n\t\n<\/p>\n<p>\n\t<span style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\u6267\u884c&nbsp;<\/span>POST<span style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">&nbsp;\u8bf7\u6c42<\/span>\n<\/p>\n<p>\n\t\n<\/p>\n<pre class=\"prettyprint lang-js\">axios.post('\/user', {\r\n    firstName: 'Fred',\r\n    lastName: 'Flintstone'\r\n  })\r\n  .then(function (response) {\r\n    console.log(response);\r\n  })\r\n  .catch(function (error) {\r\n    console.log(error);\r\n  });<\/pre>\n<p><span style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\u6267\u884c\u591a\u4e2a\u5e76\u53d1\u8bf7\u6c42<\/span> <\/p>\n<p>\n\t\n<\/p>\n<p><pre class=\"prettyprint lang-js\">function getUserAccount() {\r\n  return axios.get('\/user\/12345');\r\n}\r\n\r\nfunction getUserPermissions() {\r\n  return axios.get('\/user\/12345\/permissions');\r\n}\r\n\r\naxios.all([getUserAccount(), getUserPermissions()])\r\n  .then(axios.spread(function (acct, perms) {\r\n    \/\/ \u4e24\u4e2a\u8bf7\u6c42\u73b0\u5728\u90fd\u6267\u884c\u5b8c\u6210\r\n  }));<\/pre>\n<h2 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.75em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\taxios API<br \/>\n\t<\/h2>\n<p><span style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\u53ef\u4ee5\u901a\u8fc7\u5411&nbsp;<\/span>axios<span style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">&nbsp;\u4f20\u9012\u76f8\u5173\u914d\u7f6e\u6765\u521b\u5efa\u8bf7\u6c42<\/span>\n<\/p>\n<p><pre class=\"prettyprint lang-js\">\/\/ \u53d1\u9001 POST \u8bf7\u6c42\r\naxios({\r\n  method: 'post',\r\n  url: '\/user\/12345',\r\n  data: {\r\n    firstName: 'Fred',\r\n    lastName: 'Flintstone'\r\n  }\r\n});<\/pre>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\taxios(url[, config])<br \/>\n\t<\/h5>\n<\/p>\n<p>\n\t\t\n\t<\/p>\n<pre class=\"prettyprint lang-js\">\/\/ \u53d1\u9001 GET \u8bf7\u6c42\uff08\u9ed8\u8ba4\u7684\u65b9\u6cd5\uff09\r\naxios('\/user\/12345');<\/pre>\n<h3 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.5em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t\u8bf7\u6c42\u65b9\u6cd5\u7684\u522b\u540d<br \/>\n\t<\/h3>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u4e3a\u65b9\u4fbf\u8d77\u89c1\uff0c\u4e3a\u6240\u6709\u652f\u6301\u7684\u8bf7\u6c42\u65b9\u6cd5\u63d0\u4f9b\u4e86\u522b\u540d\n\t<\/p>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosrequestconfig_138\"><\/a>axios.request(config)<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosgeturl_config_139\"><\/a>axios.get(url[, config])<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosdeleteurl_config_140\"><\/a>axios.delete(url[, config])<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosheadurl_config_141\"><\/a>axios.head(url[, config])<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosposturl_data_config_142\"><\/a>axios.post(url[, data[, config]])<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosputurl_data_config_143\"><\/a>axios.put(url[, data[, config]])<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiospatchurl_data_config_144\"><\/a>axios.patch(url[, data[, config]])<br \/>\n\t<\/h5>\n<h6 class=\"line\" style=\"font-weight:200;font-size:1em;color:#777777;font-family:&quot;background-color:#FFFFFF;\">\n\t\t<a id=\"NOTE_146\"><\/a>NOTE<br \/>\n\t<\/h6>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u5728\u4f7f\u7528\u522b\u540d\u65b9\u6cd5\u65f6\uff0c&nbsp;url\u3001method\u3001data&nbsp;\u8fd9\u4e9b\u5c5e\u6027\u90fd\u4e0d\u5fc5\u5728\u914d\u7f6e\u4e2d\u6307\u5b9a\u3002\n\t<\/p>\n<h3 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.5em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"_149\"><\/a>\u5e76\u53d1<br \/>\n\t<\/h3>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u5904\u7406\u5e76\u53d1\u8bf7\u6c42\u7684\u52a9\u624b\u51fd\u6570\n\t<\/p>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosalliterable_153\"><\/a>axios.all(iterable)<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosspreadcallback_154\"><\/a>axios.spread(callback)<br \/>\n\t<\/h5>\n<h3 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.5em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t\u521b\u5efa\u5b9e\u4f8b<br \/>\n\t<\/h3>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u914d\u7f6e\u65b0\u5efa\u4e00\u4e2a axios \u5b9e\u4f8b\n\t<\/p>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axioscreateconfig_160\"><\/a>axios.create([config])<br \/>\n\t<\/h5>\n<pre class=\"prettyprint lang-js\">var instance = axios.create({\r\n  baseURL: 'https:\/\/some-domain.com\/api\/',\r\n  timeout: 1000,\r\n  headers: {'X-Custom-Header': 'foobar'}\r\n});<\/pre>\n<h3 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.5em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t\u5b9e\u4f8b\u65b9\u6cd5<br \/>\n\t<\/h3>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u4ee5\u4e0b\u662f\u53ef\u7528\u7684\u5b9e\u4f8b\u65b9\u6cd5\u3002\u6307\u5b9a\u7684\u914d\u7f6e\u5c06\u4e0e\u5b9e\u4f8b\u7684\u914d\u7f6e\u5408\u5e76\n\t<\/p>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosrequestconfig_174\"><\/a>axios#request(config)<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosgeturl_config_175\"><\/a>axios#get(url[, config])<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosdeleteurl_config_176\"><\/a>axios#delete(url[, config])<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosheadurl_config_177\"><\/a>axios#head(url[, config])<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosposturl_data_config_178\"><\/a>axios#post(url[, data[, config]])<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiosputurl_data_config_179\"><\/a>axios#put(url[, data[, config]])<br \/>\n\t<\/h5>\n<h5 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"axiospatchurl_data_config_180\"><\/a>axios#patch(url[, data[, config]])<br \/>\n\t<\/h5>\n<h2 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.75em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t\u8bf7\u6c42\u914d\u7f6e<br \/>\n\t<\/h2>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u8fd9\u4e9b\u662f\u521b\u5efa\u8bf7\u6c42\u65f6\u53ef\u4ee5\u7528\u7684\u914d\u7f6e\u9009\u9879\u3002\u53ea\u6709&nbsp;url&nbsp;\u662f\u5fc5\u9700\u7684\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a&nbsp;method\uff0c\u8bf7\u6c42\u5c06\u9ed8\u8ba4\u4f7f\u7528&nbsp;get&nbsp;\u65b9\u6cd5\u3002\n\t<\/p>\n<pre class=\"prettyprint lang-js\">{\r\n  \/\/ `url` \u662f\u7528\u4e8e\u8bf7\u6c42\u7684\u670d\u52a1\u5668 URL\r\n  url: '\/user',\r\n\r\n  \/\/ `method` \u662f\u521b\u5efa\u8bf7\u6c42\u65f6\u4f7f\u7528\u7684\u65b9\u6cd5\r\n  method: 'get', \/\/ \u9ed8\u8ba4\u662f get\r\n\r\n  \/\/ `baseURL` \u5c06\u81ea\u52a8\u52a0\u5728 `url` \u524d\u9762\uff0c\u9664\u975e `url` \u662f\u4e00\u4e2a\u7edd\u5bf9 URL\u3002\r\n  \/\/ \u5b83\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a `baseURL` \u4fbf\u4e8e\u4e3a axios \u5b9e\u4f8b\u7684\u65b9\u6cd5\u4f20\u9012\u76f8\u5bf9 URL\r\n  baseURL: 'https:\/\/some-domain.com\/api\/',\r\n\r\n  \/\/ `transformRequest` \u5141\u8bb8\u5728\u5411\u670d\u52a1\u5668\u53d1\u9001\u524d\uff0c\u4fee\u6539\u8bf7\u6c42\u6570\u636e\r\n  \/\/ \u53ea\u80fd\u7528\u5728 'PUT', 'POST' \u548c 'PATCH' \u8fd9\u51e0\u4e2a\u8bf7\u6c42\u65b9\u6cd5\r\n  \/\/ \u540e\u9762\u6570\u7ec4\u4e2d\u7684\u51fd\u6570\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6216 ArrayBuffer\uff0c\u6216 Stream\r\n  transformRequest: [function (data) {\r\n    \/\/ \u5bf9 data \u8fdb\u884c\u4efb\u610f\u8f6c\u6362\u5904\u7406\r\n\r\n    return data;\r\n  }],\r\n\r\n  \/\/ `transformResponse` \u5728\u4f20\u9012\u7ed9 then\/catch \u524d\uff0c\u5141\u8bb8\u4fee\u6539\u54cd\u5e94\u6570\u636e\r\n  transformResponse: [function (data) {\r\n    \/\/ \u5bf9 data \u8fdb\u884c\u4efb\u610f\u8f6c\u6362\u5904\u7406\r\n\r\n    return data;\r\n  }],\r\n\r\n  \/\/ `headers` \u662f\u5373\u5c06\u88ab\u53d1\u9001\u7684\u81ea\u5b9a\u4e49\u8bf7\u6c42\u5934\r\n  headers: {'X-Requested-With': 'XMLHttpRequest'},\r\n\r\n  \/\/ `params` \u662f\u5373\u5c06\u4e0e\u8bf7\u6c42\u4e00\u8d77\u53d1\u9001\u7684 URL \u53c2\u6570\r\n  \/\/ \u5fc5\u987b\u662f\u4e00\u4e2a\u65e0\u683c\u5f0f\u5bf9\u8c61(plain object)\u6216 URLSearchParams \u5bf9\u8c61\r\n  params: {\r\n    ID: 12345\r\n  },\r\n\r\n  \/\/ `paramsSerializer` \u662f\u4e00\u4e2a\u8d1f\u8d23 `params` \u5e8f\u5217\u5316\u7684\u51fd\u6570\r\n  \/\/ (e.g. https:\/\/www.npmjs.com\/package\/qs, http:\/\/api.jquery.com\/jquery.param\/)\r\n  paramsSerializer: function(params) {\r\n    return Qs.stringify(params, {arrayFormat: 'brackets'})\r\n  },\r\n\r\n  \/\/ `data` \u662f\u4f5c\u4e3a\u8bf7\u6c42\u4e3b\u4f53\u88ab\u53d1\u9001\u7684\u6570\u636e\r\n  \/\/ \u53ea\u9002\u7528\u4e8e\u8fd9\u4e9b\u8bf7\u6c42\u65b9\u6cd5 'PUT', 'POST', \u548c 'PATCH'\r\n  \/\/ \u5728\u6ca1\u6709\u8bbe\u7f6e `transformRequest` \u65f6\uff0c\u5fc5\u987b\u662f\u4ee5\u4e0b\u7c7b\u578b\u4e4b\u4e00\uff1a\r\n  \/\/ - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams\r\n  \/\/ - \u6d4f\u89c8\u5668\u4e13\u5c5e\uff1aFormData, File, Blob\r\n  \/\/ - Node \u4e13\u5c5e\uff1a Stream\r\n  data: {\r\n    firstName: 'Fred'\r\n  },\r\n\r\n  \/\/ `timeout` \u6307\u5b9a\u8bf7\u6c42\u8d85\u65f6\u7684\u6beb\u79d2\u6570(0 \u8868\u793a\u65e0\u8d85\u65f6\u65f6\u95f4)\r\n  \/\/ \u5982\u679c\u8bf7\u6c42\u8bdd\u8d39\u4e86\u8d85\u8fc7 `timeout` \u7684\u65f6\u95f4\uff0c\u8bf7\u6c42\u5c06\u88ab\u4e2d\u65ad\r\n  timeout: 1000,\r\n\r\n  \/\/ `withCredentials` \u8868\u793a\u8de8\u57df\u8bf7\u6c42\u65f6\u662f\u5426\u9700\u8981\u4f7f\u7528\u51ed\u8bc1\r\n  withCredentials: false, \/\/ \u9ed8\u8ba4\u7684\r\n\r\n  \/\/ `adapter` \u5141\u8bb8\u81ea\u5b9a\u4e49\u5904\u7406\u8bf7\u6c42\uff0c\u4ee5\u4f7f\u6d4b\u8bd5\u66f4\u8f7b\u677e\r\n  \/\/ \u8fd4\u56de\u4e00\u4e2a promise \u5e76\u5e94\u7528\u4e00\u4e2a\u6709\u6548\u7684\u54cd\u5e94 (\u67e5\u9605 [response docs](#response-api)).\r\n  adapter: function (config) {\r\n    \/* ... *\/\r\n  },\r\n\r\n  \/\/ `auth` \u8868\u793a\u5e94\u8be5\u4f7f\u7528 HTTP \u57fa\u7840\u9a8c\u8bc1\uff0c\u5e76\u63d0\u4f9b\u51ed\u636e\r\n  \/\/ \u8fd9\u5c06\u8bbe\u7f6e\u4e00\u4e2a `Authorization` \u5934\uff0c\u8986\u5199\u6389\u73b0\u6709\u7684\u4efb\u610f\u4f7f\u7528 `headers` \u8bbe\u7f6e\u7684\u81ea\u5b9a\u4e49 `Authorization`\u5934\r\n  auth: {\r\n    username: 'janedoe',\r\n    password: 's00pers3cret'\r\n  },\r\n\r\n  \/\/ `responseType` \u8868\u793a\u670d\u52a1\u5668\u54cd\u5e94\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u662f 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'\r\n  responseType: 'json', \/\/ \u9ed8\u8ba4\u7684\r\n\r\n  \/\/ `xsrfCookieName` \u662f\u7528\u4f5c xsrf token \u7684\u503c\u7684cookie\u7684\u540d\u79f0\r\n  xsrfCookieName: 'XSRF-TOKEN', \/\/ default\r\n\r\n  \/\/ `xsrfHeaderName` \u662f\u627f\u8f7d xsrf token \u7684\u503c\u7684 HTTP \u5934\u7684\u540d\u79f0\r\n  xsrfHeaderName: 'X-XSRF-TOKEN', \/\/ \u9ed8\u8ba4\u7684\r\n\r\n  \/\/ `onUploadProgress` \u5141\u8bb8\u4e3a\u4e0a\u4f20\u5904\u7406\u8fdb\u5ea6\u4e8b\u4ef6\r\n  onUploadProgress: function (progressEvent) {\r\n    \/\/ \u5bf9\u539f\u751f\u8fdb\u5ea6\u4e8b\u4ef6\u7684\u5904\u7406\r\n  },\r\n\r\n  \/\/ `onDownloadProgress` \u5141\u8bb8\u4e3a\u4e0b\u8f7d\u5904\u7406\u8fdb\u5ea6\u4e8b\u4ef6\r\n  onDownloadProgress: function (progressEvent) {\r\n    \/\/ \u5bf9\u539f\u751f\u8fdb\u5ea6\u4e8b\u4ef6\u7684\u5904\u7406\r\n  },\r\n\r\n  \/\/ `maxContentLength` \u5b9a\u4e49\u5141\u8bb8\u7684\u54cd\u5e94\u5185\u5bb9\u7684\u6700\u5927\u5c3a\u5bf8\r\n  maxContentLength: 2000,\r\n\r\n  \/\/ `validateStatus` \u5b9a\u4e49\u5bf9\u4e8e\u7ed9\u5b9a\u7684HTTP \u54cd\u5e94\u72b6\u6001\u7801\u662f resolve \u6216 reject  promise \u3002\u5982\u679c `validateStatus` \u8fd4\u56de `true` (\u6216\u8005\u8bbe\u7f6e\u4e3a `null` \u6216 `undefined`)\uff0cpromise \u5c06\u88ab resolve; \u5426\u5219\uff0cpromise \u5c06\u88ab rejecte\r\n  validateStatus: function (status) {\r\n    return status &gt;= 200 &amp;&amp; status &lt; 300; \/\/ \u9ed8\u8ba4\u7684\r\n  },\r\n\r\n  \/\/ `maxRedirects` \u5b9a\u4e49\u5728 node.js \u4e2d follow \u7684\u6700\u5927\u91cd\u5b9a\u5411\u6570\u76ee\r\n  \/\/ \u5982\u679c\u8bbe\u7f6e\u4e3a0\uff0c\u5c06\u4e0d\u4f1a follow \u4efb\u4f55\u91cd\u5b9a\u5411\r\n  maxRedirects: 5, \/\/ \u9ed8\u8ba4\u7684\r\n\r\n  \/\/ `httpAgent` \u548c `httpsAgent` \u5206\u522b\u5728 node.js \u4e2d\u7528\u4e8e\u5b9a\u4e49\u5728\u6267\u884c http \u548c https \u65f6\u4f7f\u7528\u7684\u81ea\u5b9a\u4e49\u4ee3\u7406\u3002\u5141\u8bb8\u50cf\u8fd9\u6837\u914d\u7f6e\u9009\u9879\uff1a\r\n  \/\/ `keepAlive` \u9ed8\u8ba4\u6ca1\u6709\u542f\u7528\r\n  httpAgent: new http.Agent({ keepAlive: true }),\r\n  httpsAgent: new https.Agent({ keepAlive: true }),\r\n\r\n  \/\/ 'proxy' \u5b9a\u4e49\u4ee3\u7406\u670d\u52a1\u5668\u7684\u4e3b\u673a\u540d\u79f0\u548c\u7aef\u53e3\r\n  \/\/ `auth` \u8868\u793a HTTP \u57fa\u7840\u9a8c\u8bc1\u5e94\u5f53\u7528\u4e8e\u8fde\u63a5\u4ee3\u7406\uff0c\u5e76\u63d0\u4f9b\u51ed\u636e\r\n  \/\/ \u8fd9\u5c06\u4f1a\u8bbe\u7f6e\u4e00\u4e2a `Proxy-Authorization` \u5934\uff0c\u8986\u5199\u6389\u5df2\u6709\u7684\u901a\u8fc7\u4f7f\u7528 `header` \u8bbe\u7f6e\u7684\u81ea\u5b9a\u4e49 `Proxy-Authorization` \u5934\u3002\r\n  proxy: {\r\n    host: '127.0.0.1',\r\n    port: 9000,\r\n    auth: : {\r\n      username: 'mikeymike',\r\n      password: 'rapunz3l'\r\n    }\r\n  },\r\n\r\n  \/\/ `cancelToken` \u6307\u5b9a\u7528\u4e8e\u53d6\u6d88\u8bf7\u6c42\u7684 cancel token\r\n  \/\/ \uff08\u67e5\u770b\u540e\u9762\u7684 Cancellation \u8fd9\u8282\u4e86\u89e3\u66f4\u591a\uff09\r\n  cancelToken: new CancelToken(function (cancel) {\r\n  })\r\n}<\/pre>\n<h2 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.75em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t\u54cd\u5e94\u7ed3\u6784<br \/>\n\t<\/h2>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u67d0\u4e2a\u8bf7\u6c42\u7684\u54cd\u5e94\u5305\u542b\u4ee5\u4e0b\u4fe1\u606f\n\t<\/p>\n<pre>{ <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ `data` \u7531\u670d\u52a1\u5668\u63d0\u4f9b\u7684\u54cd\u5e94<\/span> data: {}, <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ `status` \u6765\u81ea\u670d\u52a1\u5668\u54cd\u5e94\u7684 HTTP \u72b6\u6001\u7801<\/span> status: <span class=\"hljs-number\" style=\"color:#F5871F;\">200<\/span>, <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ `statusText` \u6765\u81ea\u670d\u52a1\u5668\u54cd\u5e94\u7684 HTTP \u72b6\u6001\u4fe1\u606f<\/span> statusText: <span class=\"hljs-string\" style=\"color:#718C00;\">'OK'<\/span>, <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ `headers` \u670d\u52a1\u5668\u54cd\u5e94\u7684\u5934<\/span> headers: {}, <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ `config` \u662f\u4e3a\u8bf7\u6c42\u63d0\u4f9b\u7684\u914d\u7f6e\u4fe1\u606f<\/span> config: {}\r\n}<\/pre>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u4f7f\u7528&nbsp;then&nbsp;\u65f6\uff0c\u4f60\u5c06\u63a5\u6536\u4e0b\u9762\u8fd9\u6837\u7684\u54cd\u5e94\uff1a\n\t<\/p>\n<pre>axios.get(<span class=\"hljs-string\" style=\"color:#718C00;\">'\/user\/12345'<\/span>)\r\n  .then(<span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color:#8959A8;\">function<\/span>(<span class=\"hljs-params\" style=\"color:#F5871F;\">response<\/span>) <\/span>{ <span class=\"hljs-built_in\" style=\"color:#F5871F;\">console<\/span>.log(response.data); <span class=\"hljs-built_in\" style=\"color:#F5871F;\">console<\/span>.log(response.status); <span class=\"hljs-built_in\" style=\"color:#F5871F;\">console<\/span>.log(response.statusText); <span class=\"hljs-built_in\" style=\"color:#F5871F;\">console<\/span>.log(response.headers); <span class=\"hljs-built_in\" style=\"color:#F5871F;\">console<\/span>.log(response.config);\r\n  });<\/pre>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u5728\u4f7f\u7528&nbsp;catch&nbsp;\u65f6\uff0c\u6216\u4f20\u9012&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Promise\/then\" target=\"_blank\">rejection callback<\/a>&nbsp;\u4f5c\u4e3a&nbsp;then&nbsp;\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u65f6\uff0c\u54cd\u5e94\u53ef\u4ee5\u901a\u8fc7&nbsp;error&nbsp;\u5bf9\u8c61\u53ef\u88ab\u4f7f\u7528\uff0c\u6b63\u5982\u5728<a href=\"https:\/\/www.kancloud.cn\/yunye\/axios\/234845#handling-errors\">\u9519\u8bef\u5904\u7406<\/a>\u8fd9\u4e00\u8282\u6240\u8bb2\u3002\n\t<\/p>\n<h2 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.75em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"defaults_352\"><\/a>\u914d\u7f6e\u7684\u9ed8\u8ba4\u503c\/defaults<br \/>\n\t<\/h2>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u4f60\u53ef\u4ee5\u6307\u5b9a\u5c06\u88ab\u7528\u5728\u5404\u4e2a\u8bf7\u6c42\u7684\u914d\u7f6e\u9ed8\u8ba4\u503c\n\t<\/p>\n<h3 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.5em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"_axios__356\"><\/a>\u5168\u5c40\u7684 axios \u9ed8\u8ba4\u503c<br \/>\n\t<\/h3>\n<pre>axios.defaults.baseURL = <span class=\"hljs-string\" style=\"color:#718C00;\">'https:\/\/api.example.com'<\/span>;\r\naxios.defaults.headers.common[<span class=\"hljs-string\" style=\"color:#718C00;\">'Authorization'<\/span>] = AUTH_TOKEN;\r\naxios.defaults.headers.post[<span class=\"hljs-string\" style=\"color:#718C00;\">'Content-Type'<\/span>] = <span class=\"hljs-string\" style=\"color:#718C00;\">'application\/x-www-form-urlencoded'<\/span>;<\/pre>\n<h3 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.5em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"_364\"><\/a>\u81ea\u5b9a\u4e49\u5b9e\u4f8b\u9ed8\u8ba4\u503c<br \/>\n\t<\/h3>\n<pre><span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u521b\u5efa\u5b9e\u4f8b\u65f6\u8bbe\u7f6e\u914d\u7f6e\u7684\u9ed8\u8ba4\u503c<\/span> <span class=\"hljs-keyword\" style=\"color:#8959A8;\">var<\/span> instance = axios.create({ <span class=\"hljs-attr\">baseURL<\/span>: <span class=\"hljs-string\" style=\"color:#718C00;\">'https:\/\/api.example.com'<\/span> }); <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u5728\u5b9e\u4f8b\u5df2\u521b\u5efa\u540e\u4fee\u6539\u9ed8\u8ba4\u503c<\/span> instance.defaults.headers.common[<span class=\"hljs-string\" style=\"color:#718C00;\">'Authorization'<\/span>] = AUTH_TOKEN;<\/pre>\n<h3 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.5em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"_376\"><\/a>\u914d\u7f6e\u7684\u4f18\u5148\u987a\u5e8f<br \/>\n\t<\/h3>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u914d\u7f6e\u4f1a\u4ee5\u4e00\u4e2a\u4f18\u5148\u987a\u5e8f\u8fdb\u884c\u5408\u5e76\u3002\u8fd9\u4e2a\u987a\u5e8f\u662f\uff1a\u5728&nbsp;lib\/defaults.js&nbsp;\u627e\u5230\u7684\u5e93\u7684\u9ed8\u8ba4\u503c\uff0c\u7136\u540e\u662f\u5b9e\u4f8b\u7684&nbsp;defaults&nbsp;\u5c5e\u6027\uff0c\u6700\u540e\u662f\u8bf7\u6c42\u7684&nbsp;config&nbsp;\u53c2\u6570\u3002\u540e\u8005\u5c06\u4f18\u5148\u4e8e\u524d\u8005\u3002\u8fd9\u91cc\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a\n\t<\/p>\n<pre><span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u4f7f\u7528\u7531\u5e93\u63d0\u4f9b\u7684\u914d\u7f6e\u7684\u9ed8\u8ba4\u503c\u6765\u521b\u5efa\u5b9e\u4f8b<\/span> <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u6b64\u65f6\u8d85\u65f6\u914d\u7f6e\u7684\u9ed8\u8ba4\u503c\u662f `0`<\/span> <span class=\"hljs-keyword\" style=\"color:#8959A8;\">var<\/span> instance = axios.create(); <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u8986\u5199\u5e93\u7684\u8d85\u65f6\u9ed8\u8ba4\u503c<\/span> <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u73b0\u5728\uff0c\u5728\u8d85\u65f6\u524d\uff0c\u6240\u6709\u8bf7\u6c42\u90fd\u4f1a\u7b49\u5f85 2.5 \u79d2<\/span> instance.defaults.timeout = <span class=\"hljs-number\" style=\"color:#F5871F;\">2500<\/span>; <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u4e3a\u5df2\u77e5\u9700\u8981\u82b1\u8d39\u5f88\u957f\u65f6\u95f4\u7684\u8bf7\u6c42\u8986\u5199\u8d85\u65f6\u8bbe\u7f6e<\/span> instance.get(<span class=\"hljs-string\" style=\"color:#718C00;\">'\/longRequest'<\/span>, { <span class=\"hljs-attr\">timeout<\/span>: <span class=\"hljs-number\" style=\"color:#F5871F;\">5000<\/span> });<\/pre>\n<h2 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.75em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"_395\"><\/a>\u62e6\u622a\u5668<br \/>\n\t<\/h2>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u5728\u8bf7\u6c42\u6216\u54cd\u5e94\u88ab&nbsp;then&nbsp;\u6216&nbsp;catch&nbsp;\u5904\u7406\u524d\u62e6\u622a\u5b83\u4eec\u3002\n\t<\/p>\n<pre><span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u6dfb\u52a0\u8bf7\u6c42\u62e6\u622a\u5668<\/span> axios.interceptors.request.use(<span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color:#8959A8;\">function<\/span> (<span class=\"hljs-params\" style=\"color:#F5871F;\">config<\/span>) <\/span>{ <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u5728\u53d1\u9001\u8bf7\u6c42\u4e4b\u524d\u505a\u4e9b\u4ec0\u4e48<\/span> <span class=\"hljs-keyword\" style=\"color:#8959A8;\">return<\/span> config;\r\n  }, <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color:#8959A8;\">function<\/span> (<span class=\"hljs-params\" style=\"color:#F5871F;\">error<\/span>) <\/span>{ <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u5bf9\u8bf7\u6c42\u9519\u8bef\u505a\u4e9b\u4ec0\u4e48<\/span> <span class=\"hljs-keyword\" style=\"color:#8959A8;\">return<\/span> <span class=\"hljs-built_in\" style=\"color:#F5871F;\">Promise<\/span>.reject(error);\r\n  }); <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u6dfb\u52a0\u54cd\u5e94\u62e6\u622a\u5668<\/span> axios.interceptors.response.use(<span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color:#8959A8;\">function<\/span> (<span class=\"hljs-params\" style=\"color:#F5871F;\">response<\/span>) <\/span>{ <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u5bf9\u54cd\u5e94\u6570\u636e\u505a\u70b9\u4ec0\u4e48<\/span> <span class=\"hljs-keyword\" style=\"color:#8959A8;\">return<\/span> response;\r\n  }, <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color:#8959A8;\">function<\/span> (<span class=\"hljs-params\" style=\"color:#F5871F;\">error<\/span>) <\/span>{ <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u5bf9\u54cd\u5e94\u9519\u8bef\u505a\u70b9\u4ec0\u4e48<\/span> <span class=\"hljs-keyword\" style=\"color:#8959A8;\">return<\/span> <span class=\"hljs-built_in\" style=\"color:#F5871F;\">Promise<\/span>.reject(error);\r\n  });<\/pre>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u5982\u679c\u4f60\u60f3\u5728\u7a0d\u540e\u79fb\u9664\u62e6\u622a\u5668\uff0c\u53ef\u4ee5\u8fd9\u6837\uff1a\n\t<\/p>\n<pre><span class=\"hljs-keyword\" style=\"color:#8959A8;\">var<\/span> myInterceptor = axios.interceptors.request.use(<span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color:#8959A8;\">function<\/span> (<span class=\"hljs-params\" style=\"color:#F5871F;\"><\/span>) <\/span>{<span class=\"hljs-comment\" style=\"color:#8E908C;\">\/*...*\/<\/span>});\r\naxios.interceptors.request.eject(myInterceptor);<\/pre>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u53ef\u4ee5\u4e3a\u81ea\u5b9a\u4e49 axios \u5b9e\u4f8b\u6dfb\u52a0\u62e6\u622a\u5668\n\t<\/p>\n<pre><span class=\"hljs-keyword\" style=\"color:#8959A8;\">var<\/span> instance = axios.create();\r\ninstance.interceptors.request.use(<span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color:#8959A8;\">function<\/span> (<span class=\"hljs-params\" style=\"color:#F5871F;\"><\/span>) <\/span>{<span class=\"hljs-comment\" style=\"color:#8E908C;\">\/*...*\/<\/span>});<\/pre>\n<h2 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.75em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"_433\"><\/a>\u9519\u8bef\u5904\u7406<br \/>\n\t<\/h2>\n<pre>axios.get(<span class=\"hljs-string\" style=\"color:#718C00;\">'\/user\/12345'<\/span>)\r\n  .catch(<span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color:#8959A8;\">function<\/span> (<span class=\"hljs-params\" style=\"color:#F5871F;\">error<\/span>) <\/span>{ <span class=\"hljs-keyword\" style=\"color:#8959A8;\">if<\/span> (error.response) { <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u8bf7\u6c42\u5df2\u53d1\u51fa\uff0c\u4f46\u670d\u52a1\u5668\u54cd\u5e94\u7684\u72b6\u6001\u7801\u4e0d\u5728 2xx \u8303\u56f4\u5185<\/span> <span class=\"hljs-built_in\" style=\"color:#F5871F;\">console<\/span>.log(error.response.data); <span class=\"hljs-built_in\" style=\"color:#F5871F;\">console<\/span>.log(error.response.status); <span class=\"hljs-built_in\" style=\"color:#F5871F;\">console<\/span>.log(error.response.headers);\r\n    } <span class=\"hljs-keyword\" style=\"color:#8959A8;\">else<\/span> { <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ Something happened in setting up the request that triggered an Error<\/span> <span class=\"hljs-built_in\" style=\"color:#F5871F;\">console<\/span>.log(<span class=\"hljs-string\" style=\"color:#718C00;\">'Error'<\/span>, error.message);\r\n    } <span class=\"hljs-built_in\" style=\"color:#F5871F;\">console<\/span>.log(error.config);\r\n  });<\/pre>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u53ef\u4ee5\u4f7f\u7528&nbsp;validateStatus&nbsp;\u914d\u7f6e\u9009\u9879\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49 HTTP \u72b6\u6001\u7801\u7684\u9519\u8bef\u8303\u56f4\u3002\n\t<\/p>\n<pre>axios.get(<span class=\"hljs-string\" style=\"color:#718C00;\">'\/user\/12345'<\/span>, { <span class=\"hljs-attr\">validateStatus<\/span>: <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color:#8959A8;\">function<\/span> (<span class=\"hljs-params\" style=\"color:#F5871F;\">status<\/span>) <\/span>{ <span class=\"hljs-keyword\" style=\"color:#8959A8;\">return<\/span> status &lt; <span class=\"hljs-number\" style=\"color:#F5871F;\">500<\/span>; <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u72b6\u6001\u7801\u5728\u5927\u4e8e\u6216\u7b49\u4e8e500\u65f6\u624d\u4f1a reject<\/span> }\r\n})<\/pre>\n<h2 class=\"line\" style=\"font-family:&quot;font-weight:200;font-size:1.75em;color:rgba(0, 0, 0, 0.87);background-color:#FFFFFF;\">\n\t\t<a id=\"_461\"><\/a>\u53d6\u6d88<br \/>\n\t<\/h2>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u4f7f\u7528&nbsp;<em>cancel token<\/em>&nbsp;\u53d6\u6d88\u8bf7\u6c42\n\t<\/p>\n<blockquote class=\"default\" style=\"color:#777777;font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n<p>\n\t\t\tAxios \u7684 cancel token API \u57fa\u4e8e<a href=\"https:\/\/github.com\/tc39\/proposal-cancelable-promises\" target=\"_blank\">cancelable promises proposal<\/a>\uff0c\u5b83\u8fd8\u5904\u4e8e\u7b2c\u4e00\u9636\u6bb5\u3002\n\t\t<\/p>\n<\/blockquote>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u53ef\u4ee5\u4f7f\u7528&nbsp;CancelToken.source&nbsp;\u5de5\u5382\u65b9\u6cd5\u521b\u5efa cancel token\uff0c\u50cf\u8fd9\u6837\uff1a\n\t<\/p>\n<pre><span class=\"hljs-keyword\" style=\"color:#8959A8;\">var<\/span> CancelToken = axios.CancelToken; <span class=\"hljs-keyword\" style=\"color:#8959A8;\">var<\/span> source = CancelToken.source();\r\n\r\naxios.get(<span class=\"hljs-string\" style=\"color:#718C00;\">'\/user\/12345'<\/span>, { <span class=\"hljs-attr\">cancelToken<\/span>: source.token\r\n}).catch(<span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color:#8959A8;\">function<\/span>(<span class=\"hljs-params\" style=\"color:#F5871F;\">thrown<\/span>) <\/span>{ <span class=\"hljs-keyword\" style=\"color:#8959A8;\">if<\/span> (axios.isCancel(thrown)) { <span class=\"hljs-built_in\" style=\"color:#F5871F;\">console<\/span>.log(<span class=\"hljs-string\" style=\"color:#718C00;\">'Request canceled'<\/span>, thrown.message);\r\n  } <span class=\"hljs-keyword\" style=\"color:#8959A8;\">else<\/span> { <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u5904\u7406\u9519\u8bef<\/span> }\r\n}); <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u53d6\u6d88\u8bf7\u6c42\uff08message \u53c2\u6570\u662f\u53ef\u9009\u7684\uff09<\/span> source.cancel(<span class=\"hljs-string\" style=\"color:#718C00;\">'Operation canceled by the user.'<\/span>);<\/pre>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\t\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a executor \u51fd\u6570\u5230&nbsp;CancelToken&nbsp;\u7684\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa cancel token\uff1a\n\t<\/p>\n<pre><span class=\"hljs-keyword\" style=\"color:#8959A8;\">var<\/span> CancelToken = axios.CancelToken; <span class=\"hljs-keyword\" style=\"color:#8959A8;\">var<\/span> cancel;\r\n\r\naxios.get(<span class=\"hljs-string\" style=\"color:#718C00;\">'\/user\/12345'<\/span>, { <span class=\"hljs-attr\">cancelToken<\/span>: <span class=\"hljs-keyword\" style=\"color:#8959A8;\">new<\/span> CancelToken(<span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color:#8959A8;\">function<\/span> <span class=\"hljs-title\" style=\"color:#4271AE;\">executor<\/span>(<span class=\"hljs-params\" style=\"color:#F5871F;\">c<\/span>) <\/span>{ <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ executor \u51fd\u6570\u63a5\u6536\u4e00\u4e2a cancel \u51fd\u6570\u4f5c\u4e3a\u53c2\u6570<\/span> cancel = c;\r\n  })\r\n}); <span class=\"hljs-comment\" style=\"color:#8E908C;\">\/\/ \u53d6\u6d88\u8bf7\u6c42<\/span> cancel();<\/pre>\n<p style=\"color:rgba(0, 0, 0, 0.87);font-family:&quot;font-size:15.96px;background-color:#FFFFFF;\">\n\t\tNote : \u53ef\u4ee5\u4f7f\u7528\u540c\u4e00\u4e2a cancel token \u53d6\u6d88\u591a\u4e2a\u8bf7\u6c42\n\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>axios \u7b80\u4ecb axios \u662f\u4e00\u4e2a\u57fa\u4e8ePromise \u7528\u4e8e\u6d4f\u89c8\u5668\u548c nodejs \u7684 HTTP \u5ba2\u6237\u7aef\uff0c\u5b83\u672c&#8230;<\/p>\n","protected":false},"author":2,"featured_media":228,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[38],"tags":[218],"_links":{"self":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/3067"}],"collection":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/comments?post=3067"}],"version-history":[{"count":1,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/3067\/revisions"}],"predecessor-version":[{"id":3068,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/3067\/revisions\/3068"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/media\/228"}],"wp:attachment":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/media?parent=3067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/categories?post=3067"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/tags?post=3067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}