![](/img/trans.png)
[英]Why AJAX GET/POST on Laravel does “Internal Server Error” when I try to use data for database request?
[英]Why i get error when try post request with headers?
我在在線商店中創建商品訂單,但是當我執行POST請求時
addToCart: function () {
axios.post('http://localhost:8081/orders/',{
quantity: '1',
product: this.id,
user: this.$store.getters.getUser._id
},{
headers: {
Authorization:"Bearer " + this.$store.getters.getToken
}
})
.then(res => {
console.log('sucsecc')
})
.catch(err => {
console.log(err)
})
}
在這里jsx代碼
<div class="descCont"><button class="add" @click="addToCart">Add to Cart</button></div>
我錯了:
POST localhost:8081/orders 404 (Not Found)
Error: Request failed with status code 404
at createError (createError.js?16d0:16)
at settle (settle.js?db52:18)
at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)
我檢查了身體中傳入數據的正確性,那里的所有數據都是正確的。 你能告訴我我錯了嗎? Node.js(orders.js)上的服務器代碼
router.post('/',checkAuth,upload.single('productImge'), (req, res, next) => {
Product.findById(req.body.productId)
.then(product => {
if (!product){
return res.status(404).json({
message: 'Product not found'
})
}
const order = new Order({
_id: mongoose.Types.ObjectId(),
quantity: req.body.quantity,
product: req.body.productId,
user: req.body.user
})
return order.save()
})
.then(result => {
res.status(201).json({
message: 'Order stored',
createdOrder: result
})
})
.catch(err => {
console.log(err)
res.status(500).json({
error: err
})
})
})
在主文件中:
const ordersRoutes = require('./routes/orders')
也許您發布的商品ID錯誤? 對於您的第一條陳述,您的第一條if語句將返回404
錯誤。
請注意, product: this.id,
可能undefined
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.