It seems cannot use jquery lib inside the x-magento-init type javascript. How to call a ajax function in x-magento-init type javascript? Thanks
1 Answer
As far as I know, we can call a ajax function in x-magento-init:
vendor/magento/module-catalog/view/frontend/templates/product/list.phtml
<script type="text/x-magento-init">
{
"[data-role=tocart-form], .form.map.checkout": {
"catalogAddToCart": {}
}
}
</script>
And then, navigate to
vendor/magento/module-catalog/view/frontend/web/js/catalog-add-to-cart.js
ajaxSubmit: function(form) {
......
$.ajax({
url: form.attr('action'),
data: form.serialize(),
type: 'post',
dataType: 'json',
beforeSend: function() {
if (self.isLoaderEnabled()) {
$('body').trigger(self.options.processStart);
}
},
success: function(res) {
......
}
});
},