我想把前端的checkBox陣列傳到后端,但是后端一直獲取不了這個陣列的資料,是否還差一步決議資料?
//【前端代碼】添加到訂單串列
let postcartData = this.$qs.stringify({
cart:this.checkBox,
},{ indices: false });
this.$axios({
method:"post",
url:'/server/api/buy/add',
data: JSON.stringify(postcartData),
headers:{
'Authorization':window.localStorage['login']
},
params:{
id:this.$store.getters.cart
}
}).then((res) => {
console.log('【添加到訂單串列除錯】'+ JSON.stringify(postcartData))
this.$toast.message('創建訂單成功');
return false;
}).catch(()=> {
this.$toast.message('創建訂單失敗');
return false;
})
/**
* 后端介面代碼
* 添加訂單串列到陣列cart
* 介面:/api/buy/add?id=adasasdadas
*/
router.post("/add",
passport.authenticate('jwt', { session: false }),
async ctx =>{
const id = ctx.query.id;
const buys = await buy.findById(id);
const newCart = {
title:ctx.request.body.title, //無法正常獲取
img:ctx.request.body.img, //無法正常獲取
price:ctx.request.body.price, //無法正常獲取
num:ctx.request.body.num ,//無法正常獲取
date:ctx.request.body.date,
};
buys.cart.unshift(newCart);
const buyUpdate = await buy.findOneAndUpdate(
{_id: id},
{$set:buys},
{new:true}
);
ctx.body = JSON.stringify(buyUpdate);
console.log(JSON.stringify(ctx.request.body))
}
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/100927.html
標籤:非技術區
