jquery ajax 用户登入 异步判断

html片段


function IbtnEnter_onclick() {
            checklogin();
            return false;
        }

        function checklogin() {
        
            if ($("#username").val() == "" || $("#username").val().length < 5 || $("#username").val().length > 30 ) {
				$("#tip").html("提示:用户名必须填写,并且长度必须在5-30个字符之间!");
                $("#username").focus();
                return false;
            }
            else{
            	$("#tip").html(" ");
            }

            if ($("#password").val() == "" || $("#password").val().length < 8 || $("#password").val().length > 20 ) {
				$("#tip").html("提示:密码必须填写,并且长度必须在8-20个字符之间!");
                $("#password").focus();
                return false;
            }
            else{
            	$("#tip").html(" ");
            }
            
            if ($("#verifycode").val().trim() == "" || $("#verifycode").val().trim() != $("#hiddencode").val().trim()) {
				$("#tip").html("提示:验证码计算有误!");
                $("#verifycode").focus();
                return false;
            }
            else{
            	$("#tip").html(" ");
            }

            $.ajax({
                type: "POST",
                url: "userLogin.do",
                dataType : 'text',
                data : $('#loginform').serialize(),  
                success: function (data) {
                    if (data == 1) {
                        location.href = "index.jsp";
                        return true;
                    }
                    else {
                        alert("登入失败,用户名或密码错误!");
                        $("#password").val("");
                        $("#verifycode").val("");
                        getverifycode();
                        $("#password").focus();
                        return false;
                    }
                }
            })
        }

    </script>


<input name="button1" type="button" class="button" id="button1" value="    登 陆    " οnclick="IbtnEnter_onclick();">


后台代码片段:

tip:

如果成功返回1

PrintWriter out = null;
						try {
							out = response.getWriter();
							out.println("1");  
					        out.flush();  
					        out.close();  
						} catch (IOException e) {
							e.printStackTrace();
						}  





扩展:

ajax另一种方式:返回json字符串

示例代码:

function del(id){
$.ajax( {
	type : "POST",
	url : "<%=request.getContextPath()%>/student.do?method=del&id=" + id,
	dataType: "json",
	success : function(data) {
		if(data.del == "true"){
			alert("删除成功!");
			$("#" + id).remove();
		}
		else{
			alert("删除失败!");
		}
		if(data.tip =="tipstr"){
			alert("tipstr");
		}
	},
	error :function(){
		alert("网络连接出错!");
	}
});
}


后台代码:
response.getWriter().print("{\"del\":\"true\",\"tip\":\"tipstr\"}");


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值