简体   繁体   English

ajax请求总是经过其他

[英]ajax request always going through else

I've been trying to validate a login form, this is my ajax code: 我一直在尝试验证登录表单,这是我的ajax代码:

<script>    
$(document).ready(function () {
/* Asociar el evento de clic del botón 'igual'
   con la lógica del negocio de la aplicación */

    $('#formlogin').submit(function() { 
        procesar();
        return false;
    });
});

function procesar() {
    var data = 'usuario=' + $('#usuario').val() + '&contrasenia=' + $('#contrasenia').val();
    $.ajax ({     
        url:  'postlogin.php', /* URL a invocar asíncronamente */
        type:    'post', /* Método utilizado para el requerimiento */
        data:     /* $('#formulario').serialize()*/data, /* Información local a enviarse con el requerimiento */

/* Que hacer en caso de ser exitoso el requerimiento */

        success:  function(response) {    
            if((response) == ""){    
                window.location = "./home";    
            }else{
                $('#mensaje').html(response);
            }      
        },

/* Que hacer en caso de que sea fallido el requerimiento */

        error:  function(response) {   

/* Limpiar cualquier resultado anterior */

            $('#mensaje').html('Error de subida');
        } 
    });
}       

</script>

and this is my php file: 这是我的php文件:

<?php

include('libreria.php');
$conn= conectar();

if(isset($_POST["usuario"])){
    $resulusuario= verificarusuario($_POST["usuario"], $_POST["contrasenia"], $conn);

    if($resulusuario->num_rows==0){
        echo "ERROR: usuario o contraseña ingresados incorrectos";    
    } else{ 

        session_start();    
        $_SESSION['usuario'] = $_POST['usuario'];           
    }
}else{
    if(isset($_POST["mensaje"])) {    
        cargartweet($_POST["idusuario"], $_POST["mensaje"], $_POST["fecha"], $conn);    
    } else {    
        if(isset($_POST["nombreusuarioseguir"])) {    
        }    
    }
}
?>

any ideas why it's always going through else of the ajax success?, I mean, even if the username and the password it's ok, it won't redirect me, and the worst thing it's that yesterday it was working... 我知道,即使用户名和密码都可以,它也无法重定向我,最糟糕的是昨天它仍然可以正常工作...

The response never equals "" . 响应永远不等于"" It's that simple. 就这么简单。 You need to do something like console.log(response===""); 您需要执行类似console.log(response===""); and you will see this for yourself. 您将自己看到这个。 It will be false. 这将是错误的。 The response might be " " or any number of things. 响应可能是" "或任何其他东西。 There is an appropriate test to run, but you need to find out what response equals that is not "" . 有一个适当的测试可以运行,但是您需要找出什么response不是""

i think your var data values usually for GET method, Try array: 我认为您的var数据值通常用于GET方法,请尝试使用数组:

var data =  { usuario: $('#usuario').val(), contrasenia: $('#contrasenia').val() };

or change type to get and replace $_POST to $_GET. 或更改类型以获取并将$ _POST替换为$ _GET。 Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM