2022-03-14 2 分钟 0.3 k0次访问
axios基础
axios的基本用法。
安装axios
基础语法
默认使用get请求
1 2 3 4
| axios('http://localhost/xxx/xxx') .then(res=>{ console.log(res); });
|
传参
get方法
1 2 3 4 5 6 7 8 9 10 11
| axios({ method:'get', url:'http://localhost/xxx/xxx', params:{ username:'zhangsan', age:10, gender:'male' } }).then(res=>{ console.log(res); });
|
post方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| axios({ method:'post', url:'http://localhost/xxx/xxx', headers:{ 'content-type':'application/x-www-form-urlencoded' } data:{ username:'zhangsan', age:10, gender:'male' } }).then(res=>{ console.log(res); });
|
实际应用语法
get请求
1 2 3 4 5 6 7
| axios.get('http://localhost/xxx?username=zhangsan').then(res=>{ console.log(res); });
axios.get('http://localhost/xxx',config:{params:{username='zhangsan'}}).then(res=>{ console.log(res); });
|
post请求
1 2 3
| axios.post('http://localhost/xxx',data:"username=zhangsan").then(res=>{ console.log(res); });
|
并发请求
结果存入数组
1 2 3 4 5 6 7 8 9 10 11
| axios.all([ axios.get('http://localhost/xxx?id=1'), axios.get('http://localhost/xxx?id=2'), axios.get('http://localhost/xxx?id=3'), ]).then(res=>{ console.log(res[0]); console.log(res[1]); console.log(res[2]); }).catch(err=>{ console.log(err); });
|
结果单独接收
1 2 3 4 5 6 7 8 9 10 11 12 13
| axios.all([ axios.get(url:'http://localhost/xxx?id=1'), axios.get(url:'http://localhost/xxx?id=2'), axios.get(url:'http://localhost/xxx?id=3'), ]).then( axios.spread((res1,res2,res3)=>{ console.log(res1); console.log(res2); console.log(res3); }) ).catch(err=>{ console.log(err); });
|