jquery ajax 登录

来源:互联网 发布:驼峰航线 知乎 编辑:程序博客网 时间:2024/05/22 20:59
  1. $('img').click(function(){  
  2.         $('img').attr('src','php/getVerify.php?'+Math.random());//刷新验证码  
  3.     })  
  4.     $('#login').click(function(){  
  5.         var username=$('#user-name').val();  
  6.         var password=$("#user-password").val();  
  7.         var code=$("#code").val();  
  8.         if(username!=""&&password!=""&&code.length==4){  
  9.             $.ajax({  
  10.                 type:"POST",  
  11.                 url:"php/userLogin.php",  
  12.                 dataType:"JSON",  
  13.                 data:{  
  14.                     "user_name":username,  
  15.                     "password":password,  
  16.                     "code":code  
  17.                 },  
  18.                 success:function(data){  
  19.                     switch(data){  
  20.                         case 1://普通用户  
  21.                             $.cookie("user",username);  
  22.                             $.cookie("limit",0);  
  23.                             window.location.href="index.php";  
  24.                             break;  
  25.                         case 2://管理员用户  
  26.                             $.cookie("user",username);  
  27.                             $.cookie("limit",1);  
  28.                             window.location.href="index.php";  
  29.                             break;  
  30.                         case 3://密码错误  
  31.                             alert("密码错误!");  
  32.                             break;  
  33.                         case 4://用户不存在  
  34.                             alert("该用户不存在!");  
  35.                             break;  
  36.                         case 0://验证码错误  
  37.                             alert("验证码不正确!");  
  38.                             break;  
  39.                     }  
  40.                       
  41.                 }  
  42.             })  
  43.         }else{  
  44.             alert("请检查您的输入!");  
  45.         }  
  46.     })  
  47.     $('#sign').click(function(){  
  48.         var username=$('#user-name').val();  
  49.         var password=$("#user-password").val();  
  50.         var code=$("#code").val();  
  51.         if(username!=""&&password!=""&&code.length==4){  
  52.             $.ajax({  
  53.                 type:"POST",  
  54.                 url:"php/addUser.php",  
  55.                 dataType:"JSON",  
  56.                 data:{  
  57.                     "user_name":username,  
  58.                     "password":password,  
  59.                     "code":code  
  60.                 },  
  61.                 success:function(data){  
  62.                     switch(data){  
  63.                         case 1://用户已存在  
  64.                             alert("该用户已存在!请换一个用户名注册。")  
  65.                             break;  
  66.                         case 2://注册成功  
  67.                             alert("注册成功!");  
  68.                             $.cookie("user",username);  
  69.                             $.cookie("limit",0);  
  70.                             window.location.href="index.php";  
  71.                             break;  
  72.                         case 0://验证码错误  
  73.                             alert("验证码不正确!");  
  74.                             break;  
  75.                     }  
  76.                       
  77.                 }  
  78.             })  
  79.         }else{  
  80.             alert("请检查您的输入!");  
  81.         }  
  82.     }) 
原创粉丝点击