Vue的异步请求

Vue的异步请求–get

new Vue({
    el :'#app',
    data:{
        obj:null
    },
    created:function(){
    this.getlist()  //可以调用自身的方法
    }
    methods:{
        getdata:function(){
            // 1.0 请求的url
            var url = 'http://vueapi.ittun.com/api/getprodlist'

            // 2.0 利用 vue-resource发出ajax的get请求
            this.$http.get(url)  //发出请求
            .then(function(response){
                this.obj = response.body;

            }) // 获取服务器响应回来的数据
        }
    }
});
</script>

Vue的异步请求–post

new Vue({
    el :'#app',
    methods:{
        postdata:function(){
            // 将数据通过vue-resource的post方法提交给http://vueapi.ittun.com/api/addproduct
             var url = 'http://vueapi.ittun.com/api/addproduct';

             // 2.0 调用 $http.post(url,传入到服务器的请求报文体的数据,{emulateJSON:true})方法
             this.$http.post(url,{name:'奔驰'},{emulateJSON:true})
             .then(function(response){
                alert(response.body.message);
             });
        }
    }
})
</script>

Vue的异步请求–jsonp

<script>
new Vue({
    el:'#app',
    data:{
        obj :null
    },
    methods:{
        getjsonp:function(){
            // 利用vue-resource中的jsonp方法实现跨域请求数据,这里注意的是
            // url后面不需要跟callback=fn这个参数了,jsonp方法会自动加上
            this.$http.jsonp('http://vueapi.ittun.com/api/jsonp')
            .then(function(response){
                // 将服务器中的数据获取赋值给obj
                this.obj = response.body;
            })
        }
    }

});
</script>
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值