function handleAjaxResponse($el) { var $header = $('header'); $el.on("ajax:success", function(e, data){ $header.after('
' + data.message + '
'); }).on("ajax:error", function(e, xhr) { if (xhr.status === 400){ $header.after('
' + xhr.responseJSON.join('
') + '
'); } else { $header.after('
Oops! There was an error processing your request. Please try again.
'); } }); } $(document).ready(function() { $('[data-id=ajax-action]').each(function(){ handleAjaxResponse($(this)); }); });