安装
npm install vue-axios --save
main.js文件
import axios from 'axios'
Vue.prototype.$http = axios
.vue文件
test(){
var testUrl='http://localhost:8088/activiti/doneTask';
this.$http.get(testUrl).then(function(response){
alert(response.data);
}).catch(function (response) {
alert("error");
})
},
请求后台接口数据(get请求和post请求)
1.get请求
- 不需要带参数的get请求
this.$axios.get(this.GLOBAL.host.+“后台接口地址”).then(res => {
//获取你需要用到的数据
})
- 需要带参数的get请求
this.$axios.get(this.GLOBAL.host.+“后台接口地址”,{
params:{
phone:12345678 //参数,键值对,key值:value值
name:hh
}
}).then(res => {
//获取你需要用到的数据
});
2.post请求
var data = {phone:12345678,name:hh} //定义一个data储存需要带的参数
this.$axios.post(this.GLOBAL.host+“后台接口地址”,this.$qs.stringify(data)
).then(res =>{
//获取你需要的数据
});
// main.js文件
import axios from "axios";
import qs from 'qs';
import Global from '../static/config/global';
Vue.prototype.$axios = axios
Vue.prototype.$qs = qs;
Vue.prototype.GLOBAL = Global;