Created
October 17, 2016 03:14
-
-
Save raank/e1f01ec76fcf2b433e677bc3a51f1c8f to your computer and use it in GitHub Desktop.
Javascript para o envio do Formulário
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '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