axios
axios是一个基于promise的网络请求库,作用于node.js和浏览器中,它是 isomorphic 的(即同一套代码可以运行在浏览器和node.js中)。在服务端它使用原生node.js http模块, 而在客户端 (浏览端) 则使用XMLHttpRequest。
使用方式
- 使用npm安装:
$ npm install axios
- 使用 cdn:
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js"></script>
基本使用
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
| <script> const btns = document.querySelectorAll('button');
btns[0].onclick = function(){ axios({ method: 'GET', url: 'http://localhost:3000/posts/1', }).then(response => { console.log(response); }); }
btns[1].onclick = function(){ axios({ method: 'POST', url: 'http://localhost:3000/posts', data: { title: "今天天气不错, 还挺风和日丽的", author: "张三" } }).then(response => { console.log(response); }); }
btns[2].onclick = function(){ axios({ method: 'PUT', url: 'http://localhost:3000/posts/2', data: { title: "今天天气不错, 还挺风和日丽的", author: "李四" } }).then(response => { console.log(response); }); }
btns[3].onclick = function(){ axios({ method: 'delete', url: 'http://localhost:3000/posts/2', }).then(response => { console.log(response); }); }
</script>
|
其他使用
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
| btns[0].onclick = function(){ axios.request({ method:'GET', url: 'http://localhost:3000/comments' }).then(response => { console.log(response); }) }
btns[1].onclick = function(){ axios.post( 'http://localhost:3000/comments', { "body": "喜大普奔", "postId": 2 }).then(response => { console.log(response); }) }
btns[3].onclick = function(){ axios.all([axios({ method: 'delete', url: 'http://localhost:3000/posts/2', }), axios({ method: 'PUT', url: 'http://localhost:3000/posts/2', data: { title: "今天天气不错, 还挺风和日丽的", author: "李四" } }) ])then(axios.spread((res1,res2) => { console.log(res1); console.log(res2); })); }
|
其余详细使用见官方文档
axios响应结果结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| { data: {},
status: 200,
statusText: 'OK',
headers: {},
config: {}, request: {} }
|
默认配置
1 2 3 4 5 6 7 8 9 10 11 12 13
| axios.defaults.method = 'GET'; axios.defaults.baseURL = 'http://localhost:3000'; axios.defaults.params = {id:100}; axios.defaults.timeout = 3000;
btns[0].onclick = function(){ axios({ url: '/posts' }).then(response => { console.log(response); }) }
|
创建实例对象发送请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <script> const duanzi = axios.create({ baseURL: 'https://api.apiopen.top', timeout: 2000 });
duanzi.get('/getJoke').then(response => { console.log(response.data) }) </script>
|
拦截器
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
| <script> axios.interceptors.request.use(function (config) { console.log('请求拦截器 成功 - 1号'); config.params = {a:100};
return config; }, function (error) { console.log('请求拦截器 失败 - 1号'); return Promise.reject(error); });
axios.interceptors.request.use(function (config) { console.log('请求拦截器 成功 - 2号'); config.timeout = 2000; return config; }, function (error) { console.log('请求拦截器 失败 - 2号'); return Promise.reject(error); });
axios.interceptors.response.use(function (response) { console.log('响应拦截器 成功 1号'); return response.data; }, function (error) { console.log('响应拦截器 失败 1号') return Promise.reject(error); });
axios.interceptors.response.use(function (response) { console.log('响应拦截器 成功 2号') return response; }, function (error) { console.log('响应拦截器 失败 2号') return Promise.reject(error); });
axios({ method: 'GET', url: 'http://localhost:3000/posts' }).then(response => { console.log('自定义回调处理成功的结果'); console.log(response); }); </script>
|
取消请求
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
| const btns = document.querySelectorAll('button');
let cancel = null;
btns[0].onclick = function(){ if(cancel !== null){ cancel(); } axios({ method: 'GET', url: 'http://localhost:3000/posts', cancelToken: new axios.CancelToken(function(c){ cancel = c; }) }).then(response => { console.log(response); cancel = null; }) }
btns[1].onclick = function(){ cancel(); }
|