dou12754 2014-11-24 15:38
浏览 56

ajax请求总是通过else

I've been trying to validate a login form, this is my ajax code:

<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

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...

  • 写回答

2条回答 默认 最新

  • dsigg21445 2014-11-24 16:33
    关注

    The response never equals "". It's that simple. You need to do something like 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 "".

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法