1.安装axios npm install axios
2.在vue 中全局配置
import axios from 'axios'
Vue.prototype.$axios = axios
3.请求示例 Get 和 Post
1)get请求有参数 参数拼接在接口后面
axios.get('/info?userId=1')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
2)get请求 参数以对象的方式传递(params:{})
axios.get('/info', {
params: {
userId: 1
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
3)Post请求
axios.post('/info', {
name: '小王',
userId: '12'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
axios.post('/info', qs.stringify({
name: '小王',
userId: '12'
}))
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
4.qs.stringify()将对象 序列化成URL的形式,以&进行拼接
let obj= {
name:'小王',
userId:'123'
}
qs.stringify(obj)
console.log(qs.stringify(obj)) // 'info=小王&userId=123'