3

I have implemented Ajax call to add product to cart, which is somewhat like below:

$.ajax({
    showLoader: true,
    url: SERVER_URL+'common/custompurchase/addtocart',
    data: data,
    type: "POST",
    dataType: 'json'
}).done(function (data) {
    // some code...
});

It is working fine. But it's not showing loading icon when i call Ajax. I have also tried below answers.

  1. $('body').trigger('processStart');
  2. $('body').loader('show');
  3. var body = $('body').loader(); body.loader('show');

In console it's showing this warning.

Expected to start loader but did not find one in the dom

Please provide suggestions if anybody has faced such issue.

Note: I have implemented this code on product detail page.

3
  • Its a default Magento bug you can check this issue on GIT : github.com/magento/magento2/issues/11904 Feb 11, 2019 at 11:54
  • but i have also coded ajax call for some other tasks on different pages. there it works fine. only on this page it does not work.
    – Khushbu
    Feb 11, 2019 at 12:08
  • Can you please add the screen when you call ajax? Feb 11, 2019 at 12:56

2 Answers 2

5

To prevent console log error use Magento default function processStart and processStop for the loader in your ajax.

remove this from your ajax call showLoader: true

 $.ajax(
         /*showLoader: true, Remove this one */
         url: url,
         data: param,
         type: "POST",
         beforeSend: function () {
             $('body').trigger('processStart'); // start loader
         }
         }).done(function (data) {
             $('body').trigger('processStop');   // stop loader
             return true;
  });
2

By adding the following HTML code before the ajax call. make sure it renders between <body> and </body>tag

<body>   
<div class="loading-mask" data-role="loader">
    <div class="loader"> &nbsp;        
    </div>
</div>
</body>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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