如何在购物车会话中使用 ajax - node js
how to use ajax in shopping cart session - node js
我想在不刷新页面的情况下插入和删除购物车中的产品。我试图在我的代码中使用 ajax 但我不明白我做错了什么。例如,这段代码应该在数据字段中插入一些东西?请帮我解答这个问题
下面是我的 ajax 代码:
$(function() {
$("#cart_id").click(function(e){
e.preventDefault();
var product;
$.ajax({
type: "GET",
data: product,
url: 'http://localhost:8888/add-to-cart-forward/:id',
success: function(response) {
console.log(response);
}
});
});
});
这是我的节点JS代码
router.get('/add-to-cart-forward/:id', function (req, res, next) {
var cart = new Cart(req.session.cart ? req.session.cart : {});
Product.findById(productId, function (err, product) {
if (err) throw err;
cart.add(product, product.id);
req.session.cart = cart;
console.log(req.session.cart);
res.render('shop/description', product);
});
});
已经完成。它现在的工作。感谢关注。我没有更改我的节点 js 文件,只更改 ajax。这是我的 ajax 代码。
$(document).on('click','#cart_id',function(e){
e.preventDefault();
var id = $(this).data('id');
$.ajax({
type: "GET",
data: id,
url: '/add-to-cart-forward/' + id,
success: function() {
console.log(id)
}
});
});
我只是更改了一些字段并分开了 "id"。我希望有人可以使用它
我想在不刷新页面的情况下插入和删除购物车中的产品。我试图在我的代码中使用 ajax 但我不明白我做错了什么。例如,这段代码应该在数据字段中插入一些东西?请帮我解答这个问题
下面是我的 ajax 代码:
$(function() {
$("#cart_id").click(function(e){
e.preventDefault();
var product;
$.ajax({
type: "GET",
data: product,
url: 'http://localhost:8888/add-to-cart-forward/:id',
success: function(response) {
console.log(response);
}
});
});
});
这是我的节点JS代码
router.get('/add-to-cart-forward/:id', function (req, res, next) {
var cart = new Cart(req.session.cart ? req.session.cart : {});
Product.findById(productId, function (err, product) {
if (err) throw err;
cart.add(product, product.id);
req.session.cart = cart;
console.log(req.session.cart);
res.render('shop/description', product);
});
});
已经完成。它现在的工作。感谢关注。我没有更改我的节点 js 文件,只更改 ajax。这是我的 ajax 代码。
$(document).on('click','#cart_id',function(e){
e.preventDefault();
var id = $(this).data('id');
$.ajax({
type: "GET",
data: id,
url: '/add-to-cart-forward/' + id,
success: function() {
console.log(id)
}
});
});
我只是更改了一些字段并分开了 "id"。我希望有人可以使用它