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]);
其他请求方式可百度一下使用;