We can make Ajax post call using few different ways;
Method 1:
jQuery.ajax({
...
});
var formData = {productId: "11", productSku: "test-sku"};
var postUrl = "http://example.com/" + "<front_name>/<controller_name>/<action>";
$.ajax({
url : postUrl,
method: "POST",
data : formData,
success: function(data, textStatus, jqXHR) {
//data - response from server
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
Method 2:
Using jQuery.post() WITHOUT .done():
var postUrl = "http://example.com" + "<front_name>/<controller_name>/<action>";
$.post(postUrl,
{productId: "11", productSku: "test-sku", submit:true},
function(data, textStatus, jqXHR) {
//data - response from server
}
).fail(function(jqXHR, textStatus, errorThrown) {
alert(textStatus);
});
Method 3:
Using jQuery.post() WITH .done() and .fail():
var postUrl = "http://example.com" + "<front_name>/<controller_name>/<action>";
$.post(postUrl,
{productId: "11", productSku: "test-sku", submit:true}
).done(function(data, textStatus, jqXHR) {
//data - response from server
}).fail(function(jqXHR, textStatus, errorThrown) {
alert(textStatus);
});