碰到一个问题弄了很长时间,后台response.write("success"),jquery的ajax可以接收到,但是因为resoponse.write()生成的是html形式的, 如果用OK来判断登录的密码和用户名是否正确,就会有问题了。所以的用response.wirte("<div>success</div>");
贴出代码:前台js:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
$(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 ;
}
}
});
});
});
$( " #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 ;
}
}
});
});
});
后台:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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();
}
{
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();
}