Doubt in the return treatment Ajax, receive data return from an API

Asked

Viewed 53 times

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 and error were replaced by done and fail, pass the user and password through the url is not very recommended, and in your data 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 the fetch or make the XHR in hand

  • 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

  • it was just an observation

  • 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 on dados it seems to me that you should check if(dados.length <= 0)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.