使用ajax jquery登录

使用ajax jquery登录,jquery,ajax,login,Jquery,Ajax,Login,我在使用ajax进行简单登录时遇到了一个问题,显然一切正常,但我不知道出了什么问题,因为我需要向客户端发送一条消息,表明用户信息是正确的,然后重定向到主页 源代码 <!DOCTYPE html> <html lang="es" > <head> <meta http-equiv='Content-type' content='text/html; charset=<? echo APP_CHARSET ?>' />

我在使用ajax进行简单登录时遇到了一个问题,显然一切正常,但我不知道出了什么问题,因为我需要向客户端发送一条消息,表明用户信息是正确的,然后重定向到主页

源代码

<!DOCTYPE html>
<html lang="es" >
    <head>
        <meta http-equiv='Content-type' content='text/html; charset=<? echo APP_CHARSET ?>' />
        <title>Red Social Basica</title>
        <? Tag::css('reset') ?>
        <? Tag::css('design') ?>
        <? echo Html::includeCss() ?>
        <? echo Tag::js('jquery/jquery.min') ?>
    </head>
    <body class="fondobody">

        <header class="topbar">
            <a href="" class="bntesatico" id="lnklogin">Login</a>
            <div id="dvlogeo">
                  User/Email<input type="text" id="txtusremail"/>
                  Password<input type="password" id="txtpassword"/>
                  <a href="" id="lnkenter">Enter</a>
            </div> 
            <a href="" class="bntesatico" id="lnkregistro">Sign up</a>

        </header>
    </body>
</html>
<script type="text/javascript">

    $(document).ready(function(){
        $('#lnklogin').click(function() {
            $('#dvlogeo').fadeToggle(200, function() {
                var divID = $('#dvlistipopubs');
                var openDiv = $(this).is(':visible') ? divID : null;
            });
            return false;
        });

        $('#lnkenter').click(function() {
            var usremail=$('#txtusremail').val(),
                password=$('#txtpassword').val();
               $.ajax({
                    type: 'POST',
                    url: "account/login.php",
                    data: 'usremail=' + usremail +
                          '&password=' + password,
                    success: function(data) {
                       if(data=='ok'){
                           document.location="account/main.php"
                       }else{
                           alert('Access denied');
                       }
                    }
                });
            return false;
        });
     });
    </script>

这是我找到的解决方案,使用JSON

在我的Ajax中:

 $.ajax({type: 'GET',
           dataType: 'json',
           url: "login.php",
           data: 'usremail=' + usremail +
                 '&clave=' + clave
           }).done(function(data) {
                            if (data.mensaje == "ok") {
                                document.location = "index.php";
                            } else {
                                $('#dvmsjlogin').html(data.mensaje);
                            }
                        });
在我的PHP中:

if ($auth->authenticate()) {
  $res['mensaje'] = 'ok';
}else{
  $res['mensaje']='error';
}

echo json_encode($res);

尝试将此代码与
函数(responseText)
;)一起使用


你能显示你的php文件吗?
console.log(data)
返回什么?在if之前(数据==…是否
success
甚至触发?如果响应不是
200OK
,则只有
complete
会触发,而不是
success
。Firebug有一个网络选项卡。检查响应。这是我的php文件,这是一个方法,因为我使用的是MVC->响应是200OK
if ($auth->authenticate()) {
  $res['mensaje'] = 'ok';
}else{
  $res['mensaje']='error';
}

echo json_encode($res);
success: function (responseText) { // Get the result and assign to each case.
        if (responseText == 'ok') {
            document.location = "account/main.php";
        } else {
            alert('Access denied');
        }
    }
});