gpt4 book ai didi

javascript - 为什么在尝试使用 header 发布请求时出现错误?

转载 作者:行者123 更新时间:2023-12-03 01:27:20 24 4
gpt4 key购买 nike

我在在线商店中创建商品订单,但是当我执行 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, 可能是未定义

关于javascript - 为什么在尝试使用 header 发布请求时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51468439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com