How to Update Minicart View Using AJAX in Magento 2

ajaxcartmagento2mini-cartview

Is there any way to update the mini cart view?

Now I'm using:

      require([
            'Magento_Customer/js/customer-data'
        ], function (customerData) {

            alert("hola");
            var sections = ['cart'];
            customerData.invalidate(sections);
        });

But it only refreshes the view by updating the page.

I'm looking for ways to make the changes reflect without having to reload the page. Any ideas?

Best Answer

You can try the below code.

require([
           'Magento_Customer/js/customer-data'
        ], function (customerData) {
            var sections = ['cart'];
            customerData.invalidate(sections);
            customerData.reload(sections, true);
        });