我对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调用时我很困惑。解释越多对我越好!
通常我们会做的是在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
},
});