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
 asked Sep 2, 2016 at 8:38
 
 
 
 Ricky.C 
 
 2,1829 gold badges34 silver badges54 bronze badges
 
 - 
 How about your issue?Khoa Truong– Khoa Truong2016年09月20日 16:36:04 +00:00Commented Sep 20, 2016 at 16:36
 - 
 Please share more code when you call AjaxMsquare– Msquare2020年02月09日 05:31:22 +00:00Commented Feb 9, 2020 at 5:31
 
1 Answer 1
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) {
 ...... 
 }
 });
 },
 
 answered Sep 2, 2016 at 11:28
 
 
 
 Khoa Truong 
 
 32.5k11 gold badges91 silver badges159 bronze badges
 
 default