1、axios概念
用于连接前后端,支持多种请求方式
特点
- 支持浏览器和node.js
- 支持promise
- 能拦截请求和响应
- 能转换请求和响应数据
- 能取消请求
- 自动转换json数据
- 浏览器端支持防止CSRF(跨站请求伪造)
2、基础使用
axios支持Promise语法,因此网络请求如下
//安装
npm install axios --save
//Vue.use(axios)
axios({
//httpbin.org可以用来做网络请求模拟,postman也可以
url:'httpbin.org'
}).then(res=>处理请求返回对象)
2.1 发起一个get请求
axios.get('http://123.207:8000/home/type=sell&page=3')
.then(res=>{
console.log(res);
})
.catch(err=>{
console.log(err);
})
另一种方法
axios({
url:'http://123.207.32.32:8000/home/type=sell&page=3',
params:{
type:'sell',
page:3
}
}).then(res=>{
console.log(res);
})
.catch(err=>{
console.log(err