vue+axios 向后台发送请求和响应数据的总结;

1. GET:

1》无参:案例:

var that=this; that.loading = true; that.finished = false;
axios.get('/api/items/news').then(function(res) { that.loading = false; that.finished = true; if (res.data.code == 0) { if( res.data.data.length>4){ for(var i=0;i<4;i++){ that.newsitems.push(res.data.data[i]); } }else{ that.newsitems = res.data.data; } }else{ that.$toast(res.data.message); } });

2》有参:

axios.get('/api/orders',{params:{status:that.status,page:page},headers:{'X-Requested-With': 'XMLHttpRequest',token:token}}).then(function(res){ that.loading = false; that.finished = true; if(res.data.code == 0){
  console.log(res.data.data);
} else{ that.$toast(res.data.message); } })

2. Post:

1》that.cart 实体类参数对象;

axios.post('/api/carts',that.cart,{headers:{'X-Requested-With': 'XMLHttpRequest',token:token}}).then(function(res){
    if(res.data.code == 0){ 
       //    console.log(res.data.data.id);
        //创建成功后,跳转
         window.location.href='/wechat/orders/create?cart_id='+res.data.data.id+'&is_fast='+num;
    }else{
        that.$toast(res.data.message);
    } 
})

2》参数指定:order_sn(名):order_sn(值),如果有多个参数使用'' , " 隔开;

axios.post('/api/orders/cancel',{order_sn:order_sn},{headers:{'X-Requested-With': 'XMLHttpRequest',token:token}}).then(function(res){
    that.loading = false;
    that.finished = true;
    if(res.data.code == 0){
        that.$toast("您已成功取消该订单!"); 
    }else{
        that.$toast(res.data.message);
    }
});

3》无参:

axios.post('/api/orders/cancel',{headers:{'X-Requested-With': 'XMLHttpRequest',token:token}}).then(

function(res){

that.loading = false;

that.finished = true;

if(res.data.code == 0){

that.$toast("您已成功取消该订单!");

}else{

that.$toast(res.data.message);

}

});

3.Delete

1》和get类似 :指定参数;例:

axios.delete(' /api/carts',{params:{id:id},headers:{'X-Requested-With': 'XMLHttpRequest',token:token}}).then(function(res){
    that.loading = false;
    that.finished = true;
    if(res.data.code == 0){
        that.$toast("删除成功!");
        that.getcartItems(page);
       // console.log(that.shopdata);
        that.shopdata.splice(e, 1); // 删除下标为i的元素,后面的1表示删除一个
        if(that.shopdata.length==0 || that.shopdata==''){
            that.nodata=true;
        }
    }else{
        that.$toast(res.data.message);
    }
});

4.Put

1》和post类似,指定对应参数;例:

axios.put('/api/orders/complated',{order_sn:order_sn},{headers:{'X-Requested-With': 'XMLHttpRequest',token:token}}).then(function(res){ 
    if(res.data.code==0){
        window.location.href = '/wechat/orders/comment?order_sn=' + order_sn;
    }else{
        that.$toast(res.data.message);

    }
});

总结:常用:

get(url[,config]);

post(url[,data[,config]]);

put(url[,data[,config]]);

delete(url[,config]);

其他请求方式可百度一下使用;

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值