1
I have an Asp.net web.api API
I am making a method that sends the login information, I am having difficulty to do the treatment of the return, even sending wrong information is passing as certain.
<script>
var usuario, senha;
$(function(){
//login
$("#envia").click(function(){
usuario = $("#txt-email").val();
senha = $("#txt-password").val();
if (usuario != "" && senha != ""){
$.ajax({type:"POST",
url:"http://appcasino.rocasafari.com.br/api/usuario/consulta/"+usuario+"/"+senha,
//url:"http://localhost:59791/api/usuario/consulta/",
//data:({usuario, senha}),
cache: false,
datatype: "json",
crossDomain: true,
beforeSend: function(){ $("#mensagem").text('Espere, por favor...');},
success: continua,
error: noConexion
});
}
});
});
/**********************************************/
function continua(dados){
console.info(dados);
if(dados=="[]"){
document.getElementById("#txt-email").value = "";
document.getElementById("#txt-password").value = "";
alert("O usuário ou sua senha está errado");
return;
}
alert("Bem vindo: "+dados);
$.mobile.changePage($("#perguntas"),"none");
}
/*****************************************/
function noConexion(){
alert("Não tem uma conexão ");
}
</script>
A remark,
success
anderror
were replaced bydone
andfail
, pass the user and password through the url is not very recommended, and in yourdata
that is commented has a syntax error:{ usuario: 'meuusuario', senha: 'minhasenha' }
, if you are using Jquery only because of AJAX recommend you do not do this, use thefetch
or make the XHR in hand– Costamilam
Thank you, but your reply did not help me, because at the time my doubt and do the treatment of the return of the consultation
– Harry
it was just an observation
– Costamilam
At some point your checkage,
if(dados=="[]")
, is valid? Because you will only show the incorrect user message and password if the condition enters this loop, if you expect to receive an array with information ondados
it seems to me that you should checkif(dados.length <= 0)
– Leandro Angelo