12

vue 学习笔记—axios(替代vue-resource)

一.使用 

1. 引入CDN的方式   https://unpkg.com/axios@0.16.2/dist/axios.min.js  或者 npm方式 npm install axios --save

2.API 

二:详情 

 mounted(){
//每次请求之前拦截
// axios拦截
axios.interceptors.request.use(function(config){
console.log('request before');
return config;
});
axios.interceptors.response.use(function(response){
console.log('respons after')
return response;
});
},

// get请求
axios.get('package.json',{params:{uesrId:'999'},headers:{token:'jack'}}).then(res=>{ //请求配置项 get('地址',{ params:{},headers:{} })
);{ 
  this.msg = res.data;
}).catch(error=>{
console.log(error);
this.msg =error;
});

//post请求
axios.post('package.json',{userId:123},{headers:{token:'opss'}}).then(res=>{ //请求配置项 post('地址', { userId:123 } , { headers:{} } )

  this.msg = res.data;
}).catch(error=>{
this.msg = error;
});

axios({
url:'package.json',
method:'post', // post 传data参数 get传 params参数 类似jQ ajax
data:{
userId:'101'
},
params:{
userId:'102'
},
headers:{
token:'http-test'
}

}).then(res=>{
this.msg =res.data;
}).catch(error=>{
console.log(error)
});

 

posted @ 2017-08-10 15:04  那片海岸  阅读(365)  评论(0编辑  收藏  举报