javascript 不知道如何将 AJAX 调用连接到控制器并使其更新对象

63lcw9qa  于 2023-09-29  发布在  Java
关注(0)|答案(1)|浏览(78)

我 AJAX 调用和控制器非常陌生,请耐心等待。
我有一些客户端的JS,我需要添加一个 AJAX 调用更新一个布尔值为真,一旦一个按钮被点击
客户端JS:

$(document).on('click', '.add-to-cart', function(e) {
    $.ajax({
        url: url,
        type: 'post',
        data: true// but I do not know what goes here
    success:(function () {//dont know what response to put in
       //I know it handles response from server but what would I put in here to update 
    }).fail(function (err) {
        console.log({err});
    });
});

控制器:

server.post(
    'updateButtonAdded',
    function (req, res, next) {
        var addToCartButtonClicked = customer.profile.custom.AddButton;// declaring the custom attribute I want to show on a profile if the button is clicked       
        if (??) {
           // I know I need to add something here to have the logic of when the button gets clicked, set the addToCartButtonClicked = true to update to the customer profile and have it checked because I set as a boolean 
        }
    next();
    }
);

我试着填写我不明白的内容,如果可以的话请协助,当涉及到控制器和 AJAX 调用时,我非常困惑。越多的解释对我越好!

js81xvg6

js81xvg61#

通常我们会做的是在ISML中设置URL,在JS文件中获取URL值,然后进行 AJAX 调用。下面是一个示例:
ISML:

<input id="yourIdHere" type="hidden" value="${URLUtils.url('YOURCONTROLLER-updateButtonAdded')}"/>

JS:

var url =  $('#yourIdHere').val();
$.ajax({
  url: url,
  method: "GET",
  success: function (data) {
    //your success code
  },
  error: function () {
    //your error code
  },
});

相关问题