日期:2014-05-16  浏览次数:20772 次

jsp + ajax验证用户登录的问题
菜鸟一只,不懂ajax。问题如下:
ajax验证用户登录,要实现登录成功则跳转到相应页面,失败则停留登录页面。但是我遇到的情况是浏览器的url会跳转到处理ajax的jsp页面,失败的话页面也会跳转到jsp页面。不懂,百度好久都没找到解决办法,请各位大神不吝赐教。附上代码:


login_in.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录界面</title>
<script type="text/javascript" src="easyui/jquery-1.7.2.min.js"></script>
<style type="text/css">
form{
width:523px; height:324px;
 margin:auto; margin-top:150px;
 background-image:url(images/login.png);
 border:1px #FFF solid;
}
table{
margin:0 auto;
margin-top:120px;
}
select,#user,#pass,#ID{
width:120px;}
.tip{
color:#F00;
font-size:12px;}
</style>
</head>

<body>
  <form id="login" action="#">  <!--登录界面-->
   <input type="hidden" name="action" value="login"/>
    <table>
     <tr><td><label for="user">用户名</label></td><td><input  name="userno" id="user" /></td><td class="tip"></td></tr>
     <tr><td><label for="pass">密码</label></td><td><input  name="password" id="pass" /></td><td class="tip"></td></tr>
     <tr><td><label for="ID">身份</label></td><td class="tip"><select name="role" id="ID">
        <option value="teacher">教师</option>
        <option value="headman">组长</option>
        <option value="student">学员</option></select></td><td></td></tr> 
    <tr><td><input type="submit" value="确认登录"  style="margin-top:10px;"/></td><td><input type="reset"  value="清空重置" style="margin-left:35px; margin-top:10px;"/></td><td></td></tr>
   </table>
  </form>
  <script type="text/javascript">
   var user=$("#uesr").val();
   var pass=$("#pass").val();
   var ID=$("#ID").val();
   $("#user,#pass,#ID").blur(function(){
   if($(this).val()==''){
   $(this).parent().next().html('请输入'+$(this).parent().prev().text());
   }
else{
 $(this).parent().next().html('');
}
   })
 /*提交验证,异步传输*/ 
  $("form").submit( function () {
    if($("#user").val()==''||$("#pass").val()==''){return false;}
else{
$.ajax({
url:'login.jsp',       //处理测试页面,注意返回内容,成功返回OK
dataType:'text',
data:$("form").serialize(),
success:function(msg){
msg = msg.replace(/rn/g,''); 
alert(msg);
if(msg=="ok"){window.location.href="main.html";}
else{