ajax部分:
<script src="jquery/jquery-3.6.0.js" type="text/javascript"></script>
<script type="text/javascript">
function GetWebserver() {
var name = $("#TextBox1").val();
var pwd = $("#TextBox2").val();
$.ajax({
url: "WebSer.asmx/bootLogin", //请求的url地址
dataType: "text", //返回格式为json
async: true, //请求是否异步,默认为异步,这也是ajax重要特性
data: { "userName": name, "password": pwd }, //参数值
contenttype: "text/json",
type: "POST", //请求方式
beforeSend: function () {
//请求前的处理
},
success: function (req) {
//请求成功时处理
alert(req);
},
complete: function () {
//请求完成的处理
},
error: function (error) {
//请求出错处理
alert(error.responseText);
}
});
}
</script>
页面标签html:
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br/>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br/>
<input id="Button1" type="button" value="button" οnclick="GetWebserver()" />
</div>
</form>
下面是asp.net:
script部分:
<script type="text/javascript">
function getLoginset() {
var userName = $get("TextBox1");
var password = $get("TextBox2");
Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback(onLoginCompleted);
Sys.Services.AuthenticationService.set_defaultFailedCallback(onFailed); //错误接收
Sys.Services.AuthenticationService.login(userName.value, password.value, true); //登录
}
function onLoginCompleted(result, userContext, methodName) {
if (result == true) {
alert("登录成功!");
} else {
alert("登录账号或密码错误!");
userName.value = "";
password.value = "";
}
}
function onFailed(error, userContext, methodName) {
// error - Sys.Net.WebServiceError对象
// userContext - 用户上下文
// methodName - 调用的方法名
alert(error.get_message());
}
</script>
HTML代码:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebSer.asmx" />
</Services>
<AuthenticationService Path="~/WebSer.asmx" />
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br/>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br/>
<asp:Button ID="Button2" runat="server" Text="登录" OnClientClick="getLoginset()" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>