文章摘要初始化中...
该文档介绍了Axios的基本用法和配置,包括创建实例、请求拦截、响应拦截以及使用Axios发送请求的示例。通过配置创建的实例可以设置请求的基本参数,如baseURL、headers和timeout。请求拦截器和响应拦截器可以在请求发送前和响应返回后进行一些操作,例如添加请求头或处理响应数据。最后,示例展示了如何在Vue项目中使用Axios发送请求,并通过async/await方式获取响应结果。
介绍作者JayHrn
生成本文简介
推荐相关文章
前往主页
看看其他文章
Axios 是一个基于 promise 网络请求库,作用于node.js 和浏览器中。 它是 isomorphic 的(即同一套代码可以运行在浏览器和node.js中)。在服务端它使用原生 node.js http 模块, 而在客户端 (浏览端) 则使用 XMLHttpRequests。 关于更多的介绍,我们可以查看官网
安装 关于使用,对于不同的项目使用方式不同,这里以npm介绍,更多安装方式可以查看 Axios 中文文档
使用 对于使用我们有许多的方式,可以直接发起请求,这里我们不做介绍,我们主要是考虑如何封装。
在vue项目中,我们新建一个文件夹,命名为utils
,以后所有的工具类都可以放在下面(我们可以把axios认为是请求数的一种工具),位置在src
路径下。
新建一个js文件,可以命名为request.js
或者axios.js
,这里我们以axios.js
命名。填写如下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 import axios from 'axios' const service = axios.create ({ baseURL : 'http://localhost:9090' , timeout : 5000 , }) service.interceptors .request .use (config => { return config }, err => { console .log (err) }) service.interceptors .response .use (response => { return response.data }, error => { return Promise .reject (error) }) export default service;
创建实例 观察如上代码,我们可以看到创建了一个axios的实例
1 2 3 4 const service = axios.create ({ baseURL : 'http://localhost:9090' , timeout : 5000 , })
这里这样的好处是不用每次请求都是创建一个axios实例。并且里面我们可以配置一些参数,如下是官网的示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 { url : '/user' , method : 'get' , baseURL : 'https://some-domain.com/api/' , transformRequest : [function (data, headers ) { return data; }], transformResponse : [function (data ) { return data; }], headers : {'X-Requested-With' : 'XMLHttpRequest' }, params : { ID : 12345 }, paramsSerializer : function (params ) { return Qs .stringify (params, {arrayFormat : 'brackets' }) }, data : { firstName : 'Fred' }, data : 'Country=Brasil&City=Belo Horizonte' , timeout : 1000 , withCredentials : false , adapter : function (config ) { }, auth : { username : 'janedoe' , password : 's00pers3cret' }, responseType : 'json' , responseEncoding : 'utf8' , xsrfCookieName : 'XSRF-TOKEN' , xsrfHeaderName : 'X-XSRF-TOKEN' , onUploadProgress : function (progressEvent ) { }, onDownloadProgress : function (progressEvent ) { }, maxContentLength : 2000 , maxBodyLength : 2000 , validateStatus : function (status ) { return status >= 200 && status < 300 ; }, maxRedirects : 5 , socketPath : null , httpAgent : new http.Agent ({ keepAlive : true }), httpsAgent : new https.Agent ({ keepAlive : true }), proxy : { protocol : 'https' , host : '127.0.0.1' , port : 9000 , auth : { username : 'mikeymike' , password : 'rapunz3l' } }, cancelToken : new CancelToken (function (cancel ) { }), decompress : true }
主要使用到的还是配置baseURL,headers,timeout等。
请求拦截 在请求之前,我们可以进行一些配置
1 2 3 4 5 6 service.interceptors .request .use (config => { return config }, err => { console .log (err) })
具体的配置自行决定
响应拦截 该部分主要是对请求之后的结果进行处理,例如登录拦截等可能会使用。
1 2 3 4 5 6 7 8 9 service.interceptors .response .use (function (response ) { return response; }, function (error ) { return Promise .reject (error); });
使用Axios 为了规范以及方便阅读请求接口,我们一般会在src
文件夹下面创建一个api
文件夹用来存储所有的请求函数。
例如创建一个plane.js
文件,来获取所有的飞机数据
1 2 3 4 5 6 7 import request from "@/utils/axios" ;export const getAllPlane = ( ) =>{ return request ({ method : "GET" , url : '/plane/list' , }) }
首先需要引入axios实例,然后配置请求。这里的请求方式为GET
,url
为/plane/list
,最终请求的路径会加上之前配置的,最终表现为http://localhost:9090/plane/list
。这里将这些函数对外暴露,最终在你需要使用的页面进行使用即可。
使用函数 Axios是支持async/await用法的,这里我们进行示例
在vue页面的methods中引入如下函数
1 2 3 4 5 6 7 8 async get ( ) { const res = await getAllPlane (); if (res.code === 200 ) { console .log (res.data ) } else { console .log (res.msg ) } }
这里res
将会获取到请求的结果。