vue 学习笔记—Resource

1.首先是引入 vue

或者用npm来安装  cnpm i vue-resource --save(推荐)ajax

3.提供的api npm

 

 

 

关于请求写法:json

get(){
// get请求
this.$http.get( // get方法里面('地址',{params:{},headers{}); 因为是get方法因此参数都要放到params里面 而后整个参数被对象包括
'package.json',
{
params:{
userId:'101'
},
headers:{
token:'abcd'
}
}
).then(
res=>{
this.msg = res.data;
},
error=>{
this.msg = error;
}
);
},
post(){
// post请求
this.$http.post( //post方法里面 post('地址',{userId:''},{headers:{}}) post方法参数直接在一个对象里面写 而后请求头也是单独一个函数里面写
'package.json',
{
userId:'102'
},
{
headers:{
access_token:'abscd'
}
}
).then(
res=>{
this.msg = res.data
}
)
}
//jsonp 请求 实际是<script>ajax传输 能解决跨域
jsonp(){
 this.$http.jsonp('http://www.imooc.com/course/AjaxCourseMembers?ids=866').then(res=>{
  this.msg = res.data;
  });
}
//在mounted里面能够写全局拦截
// 全局拦截 所有的请求
Vue.http.interceptors.push(function(res,next){
  console.log('请求前');

  next(respon=>{
 console.log('请求后');
return respon;
});
});
// methosd同级方法 http{} 能统必定义请求路径
http:{
root:'http://localhost:63342/vueactual/ops/156'
}
http 方式 相似jq ajax
ajax(){  this.$http({    url:'package.json',    params:{      userId:'123'    },    headers:{      token:'123',      tokens:'123'    },    timeout:50,    before:function(){      console.log('before init')    }  }).then(res=>{    this.msg  = res.data;  });}
相关文章
相关标签/搜索