安装axios
npm install axios
然后
npm install --save axios vue-axios
用到post请求时,需要安装qs
import qs from 'qs'
在main.js引入axios
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.prototype.axios = axios;
Vue.use(VueAxios, axios);
在页面js处引入
import axios from 'axios'
import qs from 'qs'
下面请求要加这句 withCredentials : true
get请求
axios.get(url?id=id).then(function(res) { var data = res.data; //返回的数据都在res.data里面}).catch(function(err) { console.log(err);});
post请求
var params = {'name':name,'phone':phone};var qs=require('qs');var instance = axios.create({headers: {'content-type': 'application/x-www-form-urlencoded'}});instance.post(url, qs.stringify(params)).then(function(res) { var data = res.data; //返回的数据都在res.data里面}).catch(function(err) { console.log(err);});