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

问题描述 投票:0回答:1

我对ajax调用和控制器非常陌生,请耐心等待。
我有一些客户端 JS,我需要添加一个 ajax 调用,以便在单击按钮后将布尔值更新为 true
客户端 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调用时我很困惑。解释越多对我越好!

javascript ajax controller salesforce salesforce-commerce-cloud
1个回答
0
投票

通常我们会做的是在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
  },
});
© www.soinside.com 2019 - 2024. All rights reserved.