axios的基本特性
axios是一个基于Promise用于浏览器和node.js的HTTP客户端 它具有以下特性- 支持浏览器和Node.js
- 支持Promise
- 能拦截请求和相应
- 自动转换JSON数据
axios.get('/url')
.then(ret=>{
//data属性名称是固定的,用于获取后台相应的数据
console.log(ret.data)
})
axios的常用API
- get:查询数据
- post:添加数据
- put:修改数据
- delete:删除数据
GET传递参数
- 通过URL传递参数
- 通过Parms选项传递参数
通过url传递参数
axios.get('/url?id=123')
.then(ret=>{
console.log(ret.data)
});
通过Parms传递参数
axios.get('/url',{
parms:{
id:123
}
}).then((ret)=>{
console.log(ret)
})
POST传递参数
通过选项传递参数(默认传递的是JSON格式的数据)
axios.post('/url',{
name:'李四',
pwd:123
}).then((ret)=>{
console.log(ret)
});
axios的相应结果
- data:实际相应回来的数据
- headers:响应头信息
- status:响应状态码
- statusText:响应状态信息
axios的全局配置
axios.defaults.timeout=3000; //超时时间
axios.defaults.baseURL='http://localhost:3000/app'; //默认地址
axios.defaults.headers['mytoken']='请求头'; //设置请求头