Showing Magento 2’s Loading Spinner Around Your AJAX Requests

Showing Magento 2’s Loading Spinner Around Your AJAX Requests

Users need feedback while something loads. Magento 2 already has a loader overlay you can reuse in your own scripts.

Example

Trigger processStart before the request and processStop when it finishes:

$('body').trigger('processStart');                    
$.ajax({
    url: url,
    type: "POST",
    data: formdata,
    async: true,
    contentType: false,
    showLoader: true,
    success: function (result) {                            
        $('body').trigger('processStop');
    }
});

Alternatively, pass showLoader: true in the jQuery AJAX options and Magento handles both events for you.

Want this done for you? Find out more about our web development service.

Keep reading

Related articles

All articles

Need a hand?

Stuck on something similar?
We can fix it with you.

Send us the problem. A senior developer reads every message and replies within one working day.