<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"; } }
原文链接:https://www.f2er.com/ajax/166363.html