log_in.html:
//主处理方法 $(function(){ $("#login").click(userLogin); });login.js:
function userLogin(){
//获取参数
var name=$("#count").val();
var password=$("#password").val();
//alert(name+","+password)
$("#count_msg").html("");
$("#password_msg").html("");
//格式检测
var ok=true;
if(name==""){
$("#count-msg").html("用户名不能为空");
ok=false;
}
if(password==""){
$("#password-msg").html("密码不能为空");
ok=false;
}
if(ok){//检测格式通过
////发送ajax请求
$.ajax({
ur1:path+"user/login.do",
type:"post",
data:{"name":name,"password":password},
dataType:"json",
success:function(result){
//result是服务器返回的json结果
if(result.status==0){
//将用户信息保存到cookie
var userId=result.data.cn_user_id;
addCookie("userId",userId,2);
location.href="edit.html";
}else if(result.status==1){//用户名错
$("#count-msg").html(result.msg);
}else if(result.status==2){
$("#password-msg").html(result.msg);
}
},
error: function(){
alert("登录失败!");
}
});
}
};