一、安装axios
1.如果安装了镜像,则直接输入cnpm install axios
,安装axios。
2.在main.js文件中:
var axios = require('axios') //引用axios,并设置基础URL为后台服务api地址
axios.defaults.baseURL = 'http://localhost:7002/api/'
Vue.prototype.$axios = axios // 将API方法绑定到全局
二、axios的使用
1.Get提交方式
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
2.Post提交方式
this.$axios({
method:'post',
url:'/getUser',
params:{account:"r"},
headers: {'ACCESS_TOKEN': sessionStorage.getItem("ACCESS_TOKEN")},
}).then((res) =>{
console.log(JSON.stringify(res.data));
}).catch(function(error){
if(error.message=="Network Error"){
that.$router.push("/login");
}
console.log(error);
})
this.$axios.post('/file/downloadFiles',paths,{headers: {'ACCESS_TOKEN': sessionStorage.getItem("ACCESS_TOKEN")},responseType: 'blob'})
.then((res) =>{
if(paths.length>1){
this.downloadShow(res.data,"temp.zip")
}else{
this.downloadShow(res.data,name)
}
}).catch(function(error){
if(error.message=="Network Error"){
that.$router.push("/login");
}
console.log(error);
})