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

2
  • How about your issue? Commented Sep 20, 2016 at 16:36
  • Please share more code when you call Ajax Commented Feb 9, 2020 at 5:31

1 Answer 1

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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.