vue-resource发送ajax请求

758 阅读1分钟

vue-resource ==>发送ajax请求

注意:默认vue中没有定义发送ajax请求的 事件,所以需要借助第三方插件 vue-resource

vue-resource是基于vue的,在使用vue-resource之前,要先加载vue的插件

参考文档https://github.com/pagekit/vue-resource/blob/develop/docs/http.md

get请求:
this.$http.get('请求的地址',[可选参数]).then(function(result){
    console.log(result);
});
this.$http.get('请求地址').then(=> result{
    
})
post请求:
注意:手动发起的post请求,默认没有表单格式,所以,有的服务器处理不了
      emulateJSON:true 设置提交的内容类型为普通表单数据格式
this.$http.post('请求的地址',[可选参数],[可选参数]).then(function(result){
    console.log(result);
});
this.$http.post('请求的地址',{},{emulateJSON:true}).then(function(result){
    console.log(result);
});
第一个参数:请求的url地址
第二个参数:要提交给服务器的数据,要以对象形式提交给服务器{name:'zs'}
第三个参数:配置以哪种表单数据类型提交过去
{emulateJSON:true} 以普通表单格式,将数据提交给服务器
application/x-www-form-urlencoded
jsonp请求:
实现原理:他会在Vue的prototype上绑定一个$http对象
this.$http.jsonp('请求的地址',[可选参数]).then(function(result){
    console.log(result);
});

注意:通过 $http 获取到的数据 result, 想要取得其中的数据要使用 result.body