用ajax jquery登录 [英] Login with ajax jquery

查看:64
本文介绍了用ajax jquery登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem with a simple login with ajax, apparently all is right, but I don´t know what is wrong, because I need to send a message to the client indicating that the user info is right and then redirect to main page.

源代码

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

我用firebug验证了服务器的答案,如果用户名和密码正确,则打印'ok',如果出了问题,则显示错误,问题是在这里不起作用

I used firebug to verify the answer from the server and is printing 'ok' if user and password are right and error if something is wrong, the problem is that is not working here

success: function(data) {
    if(data=='ok'){
        document.location="account/main.php"
    }else{
        alert('Access denied');
    }
}

推荐答案

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

Here's the solution I found, using 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);

这篇关于用ajax jquery登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆