Created
January 22, 2019 12:35
-
-
Save devsergioramos/005a97dc90c5824b4f4451a4c4e34827 to your computer and use it in GitHub Desktop.
Function js de post para login
This file contains 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
const submeterLogin = () => { | |
$('form#conteudo').submit(function(event) { | |
event.preventDefault(); | |
postRequestAjax(getDadosFormLogin(), successLogin(event), errorLogin()); | |
}) | |
} | |
const getDadosFormLogin = () => { | |
return JSON.parse('{"usuario" : "' + $("input#usuario").val() + '", "senha" : "' + $("input#senha").val() +'"}'); | |
} | |
const postRequestAjax = (dadosForm, success, error) => { | |
$.ajax({ | |
method: "POST", | |
type: "POST", | |
url: url, | |
contentType: "application/json; charset=utf-8", | |
data: JSON.stringify(dadosForm), | |
beforeSend: function() { | |
loading.style.display = "flex"; | |
}, | |
success: function() { | |
loading.style.display = "none"; | |
success(); | |
}, | |
error: function() { | |
error(); | |
} | |
}) | |
} | |
const successLogin = () => { | |
event.isDefaultPrevented = function() { return false; } | |
window.location.href = "dados"; | |
} | |
const errorLogin = () => { | |
$('#mensagem').css('display', 'block'); | |
$('input').removeClass('valid').addClass('error'); | |
if(err.responseJSON == undefined) { | |
$('#mensagem').html('Erro inesperado. Tente Novamente!'); | |
} else { | |
if(err.responseJSON.status == 403){ | |
$('#mensagem').html('Usuario ou Senha Incorretos!'); | |
} else if (err.responseJSON.status == 400) { | |
$('#mensagem').html('Não foi posssivel realizar login!'); | |
} else if (err.responseJSON.status == 401) { | |
$('#mensagem').html(err.responseJSON.mensagem); | |
} else { | |
$('#mensagem').html('Erro inesperado. Tente Novamente!'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment