Jquery+Json+Handler文件结合应用实例
1、页面script代码-【model数据、字符串】
<script type="text/javascript" charset="utf-8" src="Js/jquery-1.6.2.min.js"></script> <script type="text/javascript"> //提交验证 function ChekFrom() { var msg = ""; //取值 var UserName = $("#txtUserName").val(); //验证 if (UserName == "") { msg += "用户名为空!/r/n"; } else { $.ajax({ type: "POST", url: "/Controller/UserHandler.ashx", data: { Action: "IsUserName", UserName: UserName }, cache: false, //设置时候从浏览器中读取 缓存 true 表示 是 datatype: "json", async: false, success: function (result) { if (result.code == 1) { msg += "该用户名已存在!/r/n"; } } }); } if (msg == "") { return true; } else { alert(msg); return false; } } //总计金额 可以写成js方法 function BuyTotal() { var Total = $.ajax({ type: "POST", dataType: text,//xml html script json url: "/Controller/UserHandler.ashx", data: { Action: "BuyTotal" }, async: false }).responseText; return Total; } </script>
2、返回Model的handler代码
<%@ WebHandler Language="C#" Class="UserHandler" %> using System; using System.Web; using ECS.Utility; using System.Collections.Generic; //使用session的时候必须继承IRequiresSessionState接口:, System.Web.SessionState.IRequiresSessionState public class UserHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //全局默认返回信息 string Json = JsonUtil.Serialize(new { code = 0, msg = "" }); //取值 string Action = context.Request.Form["Action"] == null ? "" : context.Request.Form["Action"].ToString().Trim(); string UserName = context.Request.Form["UserName"] == null ? "" : context.Request.Form["UserName"].ToString().Trim(); //判断 if (!string.IsNullOrEmpty(Action)) { //验证用户 if (Action.Equals("IsUserName")) { //验证 if (IsExistUser(UserName)) { Json = JsonUtil.Serialize(new { code = 1, msg = "用户名已存在!" }); } else { Json = JsonUtil.Serialize(new { code = 0, msg = "用户名可以注册!" }); } } //计算金额 if (Action.Equals("BuyTotal")) { //Json格式 //Json = JsonUtil.Serialize(new { code = 1, msg = "成功", Total = 100 }); // Json = "100"; } else { //Json格式 //Json = JsonUtil.Serialize(new { code = 0, msg = "失败", Total = 0 }); Json = "0"; } } //最终返回信息 context.Response.Write(Json); } /// <summary> /// 判断UserName是否存在 /// </summary> /// <param name="userName">用户名</param> /// <returns>返回true or false</returns> public bool IsExistUser(string userName) { List<LSY.Model.A_User> UserList = new LSY.BLL.A_User().GetList(null, "UserName='" + userName.Trim() + "'and Type=0", null); if (UserList.Count > 0) { return true; } else { return false; } } public bool IsReusable { get { return false; } } }
3、页面script代码-【DataTable、List】
//查询城市 function getCity(CityVal) { var DDL_Province = $("#DDL_Province"); var DDL_City = $("#DDL_City"); DDL_City.empty(); DDL_City.append("<option value=\"0\">市/区</option>"); $.ajax( { type: "post", url: "/UserCart/Controller/CityAreas.ashx", data: { "type": "city", "provinceID": DDL_Province.val() }, dataType: "json", async: false, success: function (msg) { for (var i = 0; i < msg.length; i++) { if (CityVal == msg[i].CityName) { if (msg[i].IsCOD == 1) { DDL_City.append("<option value=" + msg[i].CityID + " selected=\"selected\">" + msg[i].CityName + "*</option>"); } else { DDL_City.append("<option value=" + msg[i].CityID + " selected=\"selected\">" + msg[i].CityName + "</option>"); } } else { if (msg[i].IsCOD == 1) { DDL_City.append("<option value=" + msg[i].CityID + " >" + msg[i].CityName + "*</option>"); } else { DDL_City.append("<option value=" + msg[i].CityID + " >" + msg[i].CityName + "</option>"); } } } getArea(''); GetAddreesSpan(); } }) };
4、返回数据集Handler代码
<%@ WebHandler Language="C#" Class="CityAreas" %> using System; using System.Web; using System.Collections.Generic; public class CityAreas : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request["type"] == "city") { context.Response.Write(select2(context.Request["provinceID"])); } else if (context.Request["type"] == "district") { context.Response.Write(select3(context.Request["cityID"])); } } public string select2(string id) { string str = string.Empty; if (!string.IsNullOrEmpty(id)) { List<ECS.Model.A_CityAreas> list = new ECS.BLL.A_CityAreas().GetList(null, "deep=2 and ParentID=" + id, null); if (list != null && list.Count > 0) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("["); foreach (ECS.Model.A_CityAreas item in list) { sb.Append("{"); sb.Append("\"CityID\":\"" + item.id + "\",\"CityName\":\"" + item.AreaName + "\",\"IsCOD\":\"" + item.IsCOD + "\""); sb.Append("},"); } sb.Remove(sb.Length - 1, 1); sb.Append("]"); str = sb.ToString(); } } return str; } public string select3(string id) { string str = string.Empty; if (!string.IsNullOrEmpty(id)) { List<ECS.Model.A_CityAreas> list = new ECS.BLL.A_CityAreas().GetList(null, "deep=3 and ParentID=" + id, null); if (list != null && list.Count > 0) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("["); foreach (ECS.Model.A_CityAreas item in list) { sb.Append("{"); sb.Append("\"DistrictID\":\"" + item.id + "\",\"DistrictName\":\"" + item.AreaName + "\",\"IsCOD\":\"" + item.IsCOD + "\""); sb.Append("},"); } sb.Remove(sb.Length - 1, 1); sb.Append("]"); str = sb.ToString(); } } return str; } public bool IsReusable { get { return false; } } }

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
2014年红帽高峰论坛随笔
版权声明:本文为半吊子子全栈工匠(wireless_com,同公众号)原创文章,未经允许不得转载。 https://blog.csdn.net/wireless_com/article/details/40348523 今年的红帽高峰论坛主题是:Transform IT,Transform Your Business 随着会议的进展,感觉还是张博士和IDC周震刚讲得较有干货,先分享一下。 当前的创新模式已经由专有模式的创新进入开源,进而转向开放创新。开放创新推动的三个方面:1.开放混合云的普及建行和中移动开始了向openstack的迁移,openstack形成了自助服务式的多租户平台。2.企业基础架构走向开放随着数据的增长(每2年数据翻翻),企业逐渐引入共有云服务。3.Devops的普及基础架构的复杂性使Devops成为必然,vendor login是令人抵触的,PaaS选择openshift(市场占有54%)还是cloud Foundary(市场占有36%)呢?开发创新带来了可靠,可持续,高成本收益,强响应和快速的服务系统。IT系统的趋势共识:大数据,云计算,社交化和移动性。IT本身...
- 下一篇
常用省市区无刷新联动实例
1、jquery代码 function getCity(CityVal) { var DDL_Province = $("#DDL_Province"); var DDL_City = $("#DDL_City"); DDL_City.empty(); DDL_City.append("<option value=\"0\">市/区</option>"); $.ajax( { type: "post", url: "/UserCart/Controller/CityAreas.ashx", data: { "type": "city", "provinceID": DDL_Province.val() }, dataType: "json", async: false, success: function (msg) { for (var i = 0; i < msg.length; i++) { if (CityVal == msg[i].CityName) { if (msg[i].IsCOD == 1) { DDL_City.append("<...
相关文章
文章评论
共有0条评论来说两句吧...