jQuery(document).ready(function($){ $('body').on( 'click', '.some-button', function(){ $.ajax( ajax_endpoint_data.api_url, { type : 'POST', dataType : 'json', data : { action: 'ajax_action', some_data: 'some_value' } }).always( function() { // Hide your spinner }).done( function( response ) { if ( ! response.success ) { // Display error message, ajax_endpoint_data.error_text } // Happy Path! Do your success message }).fail( function() { // Display error message, ajax_endpoint_data.error_text }); }); });