碰到一个问题弄了很长时间,后台response.write("success"),jquery的ajax可以接收到,但是因为resoponse.write()生成的是html形式的, 如果用OK来判断登录的密码和用户名是否正确,就会有问题了。所以的用response.wirte("<div>success</div>");
贴出代码:前台js:
代码
$(function(){
$("#logonbutton").click(function(){
var username=$("#user_name").val();
var userpwd=$("#user_pass").val();
var usercode=$("#imgCode").val();
if(username=="")
alert("用户名不能为空!")
if(userpwd=="")
alert("密码不能为空!")
if(usercode=="")
alert("验证码不能为空!")
$.ajax({
type:'post',
url:'ajaxData.aspx',
data:{"name":username,"pwd":userpwd,"code":usercode},
success:function(str)
{
$("#resultMessage").html(str);
var returntext=$("#resultMessage").text();
if(returntext.substring(0,7)=='success')
{
alert("登录成功,按确定后跳转到首页!");
location.href="index.aspx?userName="+username+"";
}
else
{
if(returntext.substring(0,6)=="用户名不存在"||returntext.substring(0,8)=="请输入有效用户名")
{//注意这里的substring(0,8)字符个数
alert("用户名不存在!");
$("#user_name").focus();
}
if(returntext.substring(0,4)=="密码错误")
{
alert("密码错误!");
$("#user_pass").focus();
}
if(returntext.substring(0,5)=="验证码错误")
{
alert("验证码错误!");
$("#imgCode").focus();
}
return;
}
}
});
});
});
后台:
代码
if (!string.IsNullOrEmpty(Request.Params["name"]) && !string.IsNullOrEmpty(Request.Params["name"]) && !string.IsNullOrEmpty(Request.Params["code"]))
{
userName = Request.Params["name"].ToString().Trim();
userPass = Request.Params["pwd"].ToString().Trim();
imgCode = Request.Params["code"].ToString().Trim();
tuser = UserDB.GetUserInfoByUID(userName);
string validateCode = "";
if (Session["num"] != null)
{
validateCode = Session["num"].ToString();//获得验证码
}
if (tuser == null)
{
Response.Write("<div>请输入有效用户名</div>");
}
else if (!tuser.FuserName.Equals(userName))
{
Response.Write("<div>用户名错误</div>");
}
else if (!tuser.FpassWord.Equals(userPass))
{
Response.Write("<div>密码错误</div>");
}
else if (!imgCode.Trim().ToLower().Equals(validateCode.ToLower()))
{
Response.Write("<div>验证码错误</div>");
}
else if (tuser != null && tuser.FpassWord.Equals(userPass))
{
Session["user"] = tuser.FuserName;
Response.Write("<div>success</div>");
//Response.Write("<div class=\"act\">ok</div>");
//Response.End();
}