Skip to content

Instantly share code, notes, and snippets.

@gustavo-rodrigues-dev
Last active December 16, 2015 10:29
Show Gist options
  • Save gustavo-rodrigues-dev/5420907 to your computer and use it in GitHub Desktop.
Save gustavo-rodrigues-dev/5420907 to your computer and use it in GitHub Desktop.
Função Ajax completa, retornando um log personalizado de erro além de uma função a ser executada ao clicar ou invocar o reset;
$('form').on('submit', function(e){
e.preventDefault();
var self = this,
name = 'Salvar teste';
$.ajax({
url: this.action,
type: 'POST',
dataType: 'JSON',
data: $(this).serialize(),
cache: false,
success: function(res){
if(res.status === 'TRA_OK'){
/*seu código*/
self.reset();
} else {
}
},
error: function(er){
var args = this.data.split('&'),
parms = {},
paramString ='';
for(var i = 0; i < args.length; i++) {
var bits = args[i].split('=');
parms[bits[0]] = bits[1];
paramString += bits[0] +' : '+ bits[1] +'\n ';
}
console.error('Error executing the function '+name+'\n'+' Status server: '+er.status+ '\n URL:' + this.url +'\n Params: \n '+paramString);
}
})
});
$('form').on('reset', function(){
$(this).hide();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment