Skip to content

Instantly share code, notes, and snippets.

@raank
Created October 17, 2016 03:14
Show Gist options
  • Select an option

  • Save raank/e1f01ec76fcf2b433e677bc3a51f1c8f to your computer and use it in GitHub Desktop.

Select an option

Save raank/e1f01ec76fcf2b433e677bc3a51f1c8f to your computer and use it in GitHub Desktop.
Javascript para o envio do Formulário
'use strict';
(function( $ ) {
$('#MyForm').submit(function() {
$.ajax({
url: 'http://localhost:8000/url/para/a/rota',
method: 'POST',
success: function( response, textStatus, jqXHR ) {
// message vem da key 'message' no response de sucesso do controller.
$('#alertas').text(response.message);
},
error: function( response, textStatus, jqXHR ) {
// response.responseText = array de erros
var error = $.parseJSON(response.responseText);
var msg = '';
var i = 0;
while (i < error.message.length) {
msg += '- ' + error.message[i] + '<br>';
i++;
}
$('#alertas').text(msg);
}
});
// para não recarregar a página.
return false;
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment