<script src ="Js/jquery-3.4.1.js"> </script>
<script>
////定义变量保存XmlHttpRequest对象
//var xmlHttp = null;
////用于创建AJAX请求核心对象XmlHttpRequest
//function createXmlHttpRequest() {
// try
// {
// xmlHttp = new XMLHttpRequest();//适用于IE7+,Fireiox,Chrome等主流浏览器
// }
// catch (Error)
// {
// xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//适用于IE低版本7以下的
// }
//}
//$(function () {
// $("#ImgButOn").click(function () {
// //调用函数创建XmlHttpRequest对象
// createXmlHttpRequest();
// //获得需要提交给服务器的数据
// var UserId = $("#TxtUser").val();
// var UserPwd = $("#TxtPwd").val();
// //服务端路劲,同时把数据传送到服务器地址拦
// var serverPath = "server.ashx?UserId=" + UserId + "&UserPwd=" + UserPwd + "";
// //设置XmlHttpRequest对象参数
// xmlHttp.open("get", serverPath, true);
// //服务端处理结果,使用XmlHttpRequest对象状态监测
// xmlHttp.onreadystatechange = clicentCallback;
// //通过XmlHttpRequest对象发送请求
// xmlHttp.send();
// })
//})
//function clicentCallback() {
// //XmlHttpRequest对象状态从开始发送请求到服务端经历了4个分别是:
// //返回值 0 :请求位初始化
// //返回值 1 :和服务端连接已经建立
// //返回值 2 : 请求已被服务端接受
// //返回值 3 :请求处理中
// //返回值 4 :请求处理完
// if (xmlHttp.readyState == 4) {
// if (xmlHttp.status == 200) {
// //获得服务端返回给客户端的结果(纯文本,xml,josn,html)
// var msg = xmlHttp.responseText;
// alert(msg);
// }
// }
//}
//#Jquery封装函数使用AJAX异步请求
$(function () {
$("#ImgButOn").click(function () {
$.ajax({
url:"server.ashx",//请求服务端地址
type:"get",//请求方式
data:{
"UserId": $("#TxtUser").val(),
"UserPwd":$("#TxtPwd").val()
}//发送到服务端的数据
success:function (date, status, xmlhttp) {//服务端成功处理时回调函数
alert(date + ":" + status);
},
error: function () {//服务端失败时的回调函数
alert("请求未成功!");
},
dataType:"text"//预期服务端发回数据类型
})
})
</script>
public void ProcessRequest(HttpContext context)
{
SES_Model.User user = new SES_Model.User();
//服务端要获得客户端以AJAX请求发送过来的数据
string UserId = context.Request.QueryString["UserId"];
string UserPwd = context.Request.QueryString["UserPwd"];
//在服务器中处理数据
user = SES_BLL.Users_Service.User_Enter(UserId, UserPwd);
string msg = string.Empty;
if (user != null)
{
msg = "ok";
}
else
{
msg = "error!";
}
context.Response.ContentType = "text/plain";
context.Response.Write(null);
}