ajax 登录

<script src="js/jquery-1.7.2.min.js"></script>
<center>
    <h2>登录页面</h2>
    用户名:<input type="text" id="name"><br>
    密码:<input type="password" id="pwd"><br>
    <button class="but">登录</button>
</center>
<script>
    $(".but").click(function(){
        //接收到的值
        var name = $("#name").val();
        var pwd = $("#pwd").val();

        //判断是否为空
        if (name == ''){
            alert("用户名不能为空");
            return;
        }
        if (pwd == ""){
            alert("密码不能为空");
            return;
        }

        //执行ajax功能
        $.ajax({
            url:"index.php?r=login/yz",
            type:"post",
            data:{
                name:name,
                pwd:pwd
            },
            success:function(res){
                if (res == 1){
                    alert("登录成功");

                    //直接跳转
                    window.location.href = "index.php?r=login/show";

                    //跳转打开一个新的页面
                    // window.open("index.php?r=login/show");
                } else{
                    alert("登录失败");
                }
            }
        })
    })
</script>

  

posted @ 2019-07-08 11:00  久醉121  阅读(93)  评论(0编辑  收藏  举报