{"id":7977,"date":"2020-04-11T22:30:38","date_gmt":"2020-04-11T14:30:38","guid":{"rendered":"https:\/\/sdeno.com\/?p=7977"},"modified":"2022-11-10T18:23:35","modified_gmt":"2022-11-10T10:23:35","slug":"axios%e5%b8%b8%e7%94%a8%e5%8f%82%e6%95%b0%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=7977","title":{"rendered":"axios\u5e38\u7528\u53c2\u6570\u8be6\u89e3"},"content":{"rendered":"<p>vue axios<\/p>\n<p>Vue-resource\u5df2\u7ecf\u4e0d\u7ef4\u62a4\u4e86\uff0c\u73b0\u5728axios\u5df2\u7ecf\u6210\u4e3avue\u7684\u6807\u914d<\/p>\n<pre>    \/\/<span style=\"color: #ff0000;\">\u5168\u5c40 \u62e6\u622a\u5668<\/span>\r\n\/\/       \u62e6\u622a\u5668\u4e00\u822c\u505a\u4ec0\u4e48\uff1f\r\n\/\/\r\n\/\/ \u3000\u3000   1. \u4fee\u6539\u8bf7\u6c42\u5934\u7684\u4e00\u4e9b\u914d\u7f6e\u9879\r\n\/\/        2. \u7ed9\u8bf7\u6c42\u7684\u8fc7\u7a0b\u6dfb\u52a0loadin\r\n\/\/        3. \u7ed9\u8bf7\u6c42\u6dfb\u52a0\u53c2\u6570\r\n\/\/        4. \u6743\u9650\u9a8c\u8bc1\r\n  this.$axios.create({\r\n    \/\/ headers: {\r\n    \/\/   'content-type': 'application\/json; charset=utf-8'\r\n    \/\/ },\r\n       baseURL: '\/api', \/\/ url = base url + request url\r\n    \/\/timeout: 10000 \/\/ request timeout\r\n  })\r\n\r\n this.$axios.interceptors.request.use(config=&gt;{\r\n        console.log(config);\r\n        config.headers.xx='11'\r\n\r\n        <span style=\"color: #ff0000;\">\/\/\u6839\u636epost\u548cget\u8bf7\u6c42\u4f20\u516c\u5171\u53c2\u6570<\/span>\r\n        if(config.method === 'post') {\r\n\r\n          let data = Qs.parse(config.data) \/\/\u5c06aa=11&amp;bb=22\u8f6c{aa=11,bb=22}\r\n          config.data = Qs.stringify({ \/\/\u8d4b\u503c\u7ed9\u7ed3\u6784\u662f  aa=11&amp;bb=22\r\n            cmsId: JSON.parse(localStorage.getItem('uesrinfo')).id,\r\n            ...data\r\n          })\r\n\r\n         if( !(\/cmsId\/ig.test(config.url)) ){\r\n           config.params = {\r\n           cmsId: JSON.parse(localStorage.getItem('uesrinfo')).id,\r\n             ...config.params\r\n           }\r\n         }\r\n\r\n       } else if(config.method === 'get') {\r\n         if( !(\/cmsId\/ig.test(config.url)) ){\r\n           config.params = {\r\n             cmsId: JSON.parse(localStorage.getItem('uesrinfo')).id,\r\n             ...config.params\r\n           }\r\n         }\r\n       }\r\n\r\n        return config\r\n      },err=&gt;{\r\n        console.log(err)\r\n })\r\n\r\n\r\n  \/\/ \u54cd\u5e94\u62e6\u622a\u5668\r\n this.$axios.interceptors.response.use((response) =&gt; {\r\n\r\n     if(response.config.method == 'post'){\r\n       Loading.service().close();  \/\/\u63a5\u53e3\u8bf7\u6c42\u5b8c\u6210\u540e\u5173\u95edloading\r\n     }\r\n\r\n     return response\r\n }, (error) =&gt; {\r\n     Loading.service().close();\r\n     return Promise.reject(error)\r\n })\r\n\r\n\r\n  this.$axios({\r\n        method: 'post',\r\n        url: 'www.xgllseo.com\/',\r\n\r\n        <span style=\"color: #ff0000;\"><strong>\/\/\u5982\u679curl\u53c2\u6570\u5e26\u6709http\u3001https\u5219\u6ca1\u6548\u679c\uff0c\u5426\u5219\u5c31\u4f1a\u662fhttp:\/\/www.baidu.com+url\u7684\u8def\u5f84<\/strong><\/span>\r\n        baseURL:'http:\/\/www.baidu.com',\r\n\r\n        <span style=\"color: #ff0000;\">\/\/\u53ea\u6709get\u65f6\u624d\u6709\u6548\uff0chttp:\/\/xxx\/com\/1?id=12345<\/span>\r\n        params: {ID: 12345},\r\n\r\n        <span style=\"color: #ff0000;\">\/\/\u53ea\u6709put\/post\/patch\u8bf7\u6c42\u7c7b\u578b\u65f6\uff0c\u8bf7\u6c42\u4f53\u624d\u80fd\u4f20\u6570\u636e\u7ed9\u540e\u53f0\uff0c\r\n        \/\/ \u4e14\u6570\u636e\u7c7b\u578b\u53ea\u80fd\u662fFormData(\u7c7b\u4f3cinput\u91cc\u9762\u7684\u6570\u636e), File(\u7c7b\u4f3c\u9009\u4e2d\u7684\u56fe\u7247\u3001rar\u7b49), Blob(\u7c7b\u4f3c\u89c6\u9891\u3001\u97f3\u9891)<\/span>\r\n        data: {\r\n          firstName: 'Fred',\r\n          lastName: 'Flintstone'\r\n        },\r\n        \r\n        \/\/<span style=\"color: #ff0000;\">\u8bbe\u7f6e\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668\u7684\u662f\u4ec0\u4e48\u6570\u636e\u7c7b\u578b<\/span>\r\n        \/\/ \u53ef\u9009\u9879 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'\r\n        responseType: 'json', \/\/ \u9ed8\u8ba4\u503c\u662fjson,\r\n        <span style=\"color: #ff0000;\">\/\/\u8bf7\u6c42\u5934\u7528\u6765\u4f20token\u5c45\u591a<\/span>\r\n        headers: {'Content-Type': 'application\/json'},\r\n\r\n        <span style=\"color: #ff0000;\">\/\/\u53ef\u4ee5\u7406\u89e3\u4e3a\u5c40\u90e8\u62e6\u622a\u5668\uff0c\u5728\u5168\u5c40\u7684\u62e6\u622a\u5668\u4e2d\u52a0\u4e86\u6211\u4eec\u4e0d\u9700\u8981\u7684\u5b57\u6bb5\uff0c\u53ef\u4ee5\u5728\u6b64\u5904\u518d\u6b21\u7f16\u8f91<\/span>\r\n         transformRequest: (data, headers) =&gt; {\r\n          delete headers.common.AUTHORIZATION \/\/\u4f8b\u5982\u5220\u9664\u4e86 request.headers.common={AUTHORIZATION:'xxx'}\r\n          return data\r\n        },\r\n\r\n\r\n        <span style=\"color: #ff0000;\">\/\/\u62e6\u622a\u5668\uff0c\u9009\u9879\u5141\u8bb8\u6211\u4eec\u5728\u6570\u636e\u4f20\u9001\u5230`then\/catch`\u65b9\u6cd5\u4e4b\u524d\u5bf9\u6570\u636e\u8fdb\u884c\u6539\u52a8<\/span>\r\n        transformResponse: [function (data) {\r\n          \/\/ Do whatever you want to transform the data\r\n\r\n          return data;\r\n        }]\r\n\r\n  }).then(function(response){\r\n       console.log(response);\r\n  }).catch(function(err){\r\n       console.log(err);\r\n  });;<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">axios\u4e2dx-www-form-urlencoded\u683c\u5f0f\u63d0\u4ea4post\u8bf7\u6c42<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">axios({\r\n        url: `xxxx`,\r\n        method: 'post',\r\n        data,\r\n        \/\/ \u5229\u7528 transformRequest \u8fdb\u884c\u8f6c\u6362\u914d\u7f6e\r\n        transformRequest: [\r\n            function(oldData) {\r\n                \/\/ console.log(oldData)\r\n                let newStr = '';\r\n                for (let item in oldData) {\r\n                    newStr += encodeURIComponent(item) + '=' + encodeURIComponent(oldData[item]) + '&amp;';\r\n                }\r\n                newStr = newStr.slice(0, -1);\r\n                return newStr;\r\n            }\r\n        ],\r\n        headers: {\r\n            'Content-Type': 'application\/x-www-form-urlencoded'\r\n        }\r\n});\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u53c2\u8003<a href=\"https:\/\/www.jianshu.com\/p\/7a9fbcbb1114\" target=\"_blank\" rel=\"noopener\">https:\/\/www.jianshu.com\/p\/7a9fbcbb1114<\/a><\/p>\n<p>\u5982\u679c\u9047\u5230\u8de8\u57df\u95ee\u9898\uff0c\u8bf7\u4f7f\u7528\u4ee3\u7406<a href=\"https:\/\/sdeno.com\/?p=7834\" target=\"_blank\" rel=\"noopener\">https:\/\/sdeno.com\/?p=7834<\/a><\/p>\n<p>\u8981\u4e48\u7136\u540e\u53f0\u52a0\u4e00\u4e2a\u8bf7\u6c42\u5934\u3002<a href=\"https:\/\/sdeno.com\/?p=4815\" target=\"_blank\" rel=\"noopener\">https:\/\/sdeno.com\/?p=4815<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>vue axios Vue-resource\u5df2\u7ecf\u4e0d\u7ef4\u62a4\u4e86\uff0c\u73b0\u5728axios\u5df2\u7ecf\u6210\u4e3avue\u7684\u6807\u914d \/\/\u5168\u5c40 \u62e6\u622a\u5668 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-7977","post","type-post","status-publish","format-standard","hentry","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/7977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7977"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/7977\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}