如何用jQuery的Ajax实现无验证码登录系统的用户名和密码验证?
- 内容介绍
- 文章标签
- 相关推荐
本文共计188个文字,预计阅读时间需要1分钟。
使用Ajax验证登录系统
javascript$(function(){ $('#loginForm').submit(function(event){ event.preventDefault(); var formData=$(this).serialize(); $.ajax({ type: 'POST', url: '/login', data: formData, success: function(response){ if(response.success){ window.location.href=response.redirectUrl; } else { $('#error').text(response.message); } } }); });});
ajax方式验证登录系统/** * */ $(function(){ $('#sel_user').bind('keypress',function(event){ if(event.keyCode == '13'){ $("password").focus(); } }); $('#password').bind('keypress',function(event){ if(event.keyCode == '13'){ login_act(); } }); $('#loading').hide(); }); /*登录*/ function login_act() { var user = $('#sel_user').val(); if (user == '') { alert('用户不能为空'); $('#userid').focus(); return; } var pwd = $.trim($("#password").val()); if (pwd == '') { alert('密码不能为空'); $('#password').focus(); return; } $('#loading').show(); $.ajax({ type : "POST", url : "login_validate.php", data : "act=login" + "&userid=" + user + '&password=' + pwd, dataType : "json", async : false, cache : false, success : function(data) { $('#loading').hide(); if (data['ans'] == 'ok') { // window.location.href = '../main/index.php';//直接跳转 //延迟100毫秒 跳转 setTimeout(function(){ window.location.href = '../main/index.php'; return false;//阻止冒泡 },100); } else { alert(data['ans']); return false; } }, error : function(e, jqxhr, settings, exception) { $('#loading').hide(); alert('服务器繁忙'); } }); }
本文共计188个文字,预计阅读时间需要1分钟。
使用Ajax验证登录系统
javascript$(function(){ $('#loginForm').submit(function(event){ event.preventDefault(); var formData=$(this).serialize(); $.ajax({ type: 'POST', url: '/login', data: formData, success: function(response){ if(response.success){ window.location.href=response.redirectUrl; } else { $('#error').text(response.message); } } }); });});
ajax方式验证登录系统/** * */ $(function(){ $('#sel_user').bind('keypress',function(event){ if(event.keyCode == '13'){ $("password").focus(); } }); $('#password').bind('keypress',function(event){ if(event.keyCode == '13'){ login_act(); } }); $('#loading').hide(); }); /*登录*/ function login_act() { var user = $('#sel_user').val(); if (user == '') { alert('用户不能为空'); $('#userid').focus(); return; } var pwd = $.trim($("#password").val()); if (pwd == '') { alert('密码不能为空'); $('#password').focus(); return; } $('#loading').show(); $.ajax({ type : "POST", url : "login_validate.php", data : "act=login" + "&userid=" + user + '&password=' + pwd, dataType : "json", async : false, cache : false, success : function(data) { $('#loading').hide(); if (data['ans'] == 'ok') { // window.location.href = '../main/index.php';//直接跳转 //延迟100毫秒 跳转 setTimeout(function(){ window.location.href = '../main/index.php'; return false;//阻止冒泡 },100); } else { alert(data['ans']); return false; } }, error : function(e, jqxhr, settings, exception) { $('#loading').hide(); alert('服务器繁忙'); } }); }

