您的位置:首页 > 其它

Ajax无刷新登陆

2013-05-20 19:35 120 查看
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="Jquery1.7.js"></script>
<script type="text/javascript">
$(function () {
$('#btnlogin').click(function () {
$.ajax({
type: "post", //客户端向服务器发送请求时采取的方式
contentType: "application/json", //指定客户端发送给服务器的内容的类型以及服务器返回给客户端内容的类型为json格式
url: "WebService1.asmx/logindb",//指明客户端要向哪个页面里面的哪个方法发送请求
data: "{username:'" + $('#txtUserName').val() + "',password:'" + $('#txtPassWord').val() + "'}",//指定伴随发送的请求传递到服务器的参数
success: function (result) {//客户端调用服务器端方法成功后执行的回调函数
if (result.d=="true") {
alert("登陆成功");
}
else {
alert("登录失败");
}
}
})
})
})
</script>
</head>
<body>
<table>
<tr>
<td>账号: </td>
<td>
<input id="txtUserName" type="text" />
</td>
</tr>
<tr>
<td>密码: </td>
<td>
<input id="txtPassWord" type="text" />
</td>
</tr>
<tr>
<td colspan="2">
<input id="btnlogin" type="button" value="登陆" /><span></span></td>
</tr>
</table>
</body>

</html>

WebService1.asmx 正在这里调用了web 服务

[WebMethod]
public string logindb(string username, string password)
{
string sqlstr = "Data Source=APPLE;Initial Catalog=Jquery;User ID=sa;Password=zhang123";
SqlConnection conn = new SqlConnection(sqlstr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select COUNT(*) from T_user where UUserName=@name and UPassword=@pwd";
cmd.Parameters.AddWithValue("@name ",username);
cmd.Parameters.AddWithValue("@pwd",password));
int a = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
conn.Dispose();
if (a>0)
{
return "true";
}
else
{
return "false";
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: