1

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

0

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) {
               ......  
            }
        });
    },

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.