C#一般处理程序与json传递
程序员文章站
2022-06-03 21:18:57
...
前端页面
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="../Scripts/jquery-1.10.2.min.js"></script>
<title></title>
</head>
<body>
<input type="button" id="btn4" value="post多个参数" />
</body>
</html>
<script>
$("#btn4").click(function () {
//接交数据
$.post("Handler/HandlerLogin.ashx", { "StationID": "10001", "UserID": "101", "UserPwd": '12345' },
function (data, status) {//接收返回数据
data = $.parseJSON(data);
alert(data[0]["StationID"]);//取出json对象数组
//alert(data.ID);//取出临时json对象
//alert(data["ID"]);
});
</script>
后端处理程序,如下:
public class HandlerLogin : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//接收前端json数据
string StationID = context.Request["StationID"];
string UserID = context.Request["UserID"];
string UserPwd = context.Request["UserPwd"];
//返回json数据给前端
List<loginInfo> lists = new List<loginInfo> {
new loginInfo{ StationID ="10001", UserID ="101"},
new loginInfo{ StationID ="10002", UserID="102"}
};
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(lists));
//HttpContext.Current.Response.Write(JsonConvert.SerializeObject(new { ID="1200" } ));//临时组合json
//string strSql = $"select count(*) from LoginUser where (StationID='{info.StationID}' and UserID ='{ info.UserID}' and UserPwd ='{ info.UserPwd}') or " +
//$"(StationID = '{info.StationID}' and UserName = '{ info.UserID}' and UserPwd = '{ info.UserPwd}')";
//object obj = SQLHelper.GetSingleResult(strSql);
//if (obj != null)
//{
//}
//else
//
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
后端为C#一般处理程序