Magento – Expected to start loader but did not find one in the dom – Ajax Call Magento 2

addtocartajaxmagento2

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.

Best Answer

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;
  });
Related Topic