Validate jQuery, send data with submitHandler with sweet alert

1

the sweet alert does not shoot at me. Maybe it's because of the event. If we comment on the sweet alert and decompose a normal alert if it tells me the message.

In my index, I log in, send data to a servlet, recover the sevlet response and, if your credentials are correct, I send it 1 and redirect to menu, ..., but, because bad credentials.

  $(document).ready(function(){
    $("#FRM_Login").validate({
        rules : {
            usuario : {
                required : true,
                minlength : 5,
                maxlength : 25
            },
            pass : {
                required : true,
                minlength : 8,
                maxlength : 12
            }
        },
        messages : {
            usuario : {
                required : "El campo usuario es obligatorio",
                minlength : "El usuario debe contener al menos 5 caracteres",
                maxlength : "El usuario debe contener no mas de 25 caracteres"
            },
            pass : {
                required : "El campo password es obligatorio",
                minlength : "La contraseña debe contener al menos 8 caracteres",
                maxlength : "La contraseña debe contener no mas de 12 caracteres"
            }
        },
        submitHandler: function (form){
            var data = $("#FRM_Login").serialize();
            $.post("Login", data, function (respuesta, estado, jqXHR){
                if(respuesta === '1'){
                    $("#BTN_Login").onclick = function(){
                        swal({
                            title: "Credenciales Correctas",
                            text: "Redireccionando...",
                            type: "success",
                            timer: 1000,
                            showConfirmButton: false
                        });
                    };
                    //alert("Credenciales Correctas");
                    var delay = 2000;
                    setTimeout(function(){ window.location = "Menu.jsp"; }, delay);
                }else{
                    $(document).ready(function(){ 
                        swal("Oops...", "Credenciales Incorrectas!", "error");
                    };
                    //alert("Credenciales Incorrectas");
                }
                $("#usuario").val("");
                $("#pass").val("");
            });
        }
    }); });
    
asked by William Vasquez 20.09.2016 в 09:57
source

1 answer

1

I do not see the need for .onclick when respuesta === 1 (data has already been sent and you are only going to redirect) and when this condition is false, normally $(document).ready does not work on ajax requests:

        submitHandler: function (form){
        var data = $("#FRM_Login").serialize();
        $.post("Login", data, function (respuesta, estado, jqXHR){
            if(respuesta === '1'){

                    swal({
                        title: "Credenciales Correctas",
                        text: "Redireccionando...",
                        type: "success",
                        timer: 1000,
                        showConfirmButton: false
                    });

                var delay = 2000;
                setTimeout(function(){ window.location = "Menu.jsp"; }, delay);
            }else{

                    swal("Oops...", "Credenciales Incorrectas!", "error");

            }
            $("#usuario").val("");
            $("#pass").val("");
        });
    
answered by 20.09.2016 / 14:29
source