1、页面script代码-【model数据、字符串】

  1. <script type="text/javascript" charset="utf-8" src="Js/jquery-1.6.2.min.js"></script>
  2. <script type="text/javascript">
  3. //提交验证
  4. function ChekFrom()
  5. {
  6. var msg = "";
  7. //取值
  8. var UserName = $("#txtUserName").val();
  9. //验证
  10. if (UserName == "") {
  11. msg += "用户名为空!/r/n";
  12.  
  13. } else {
  14.  
  15. $.ajax({
  16. type: "POST",
  17. url: "/Controller/UserHandler.ashx",
  18. data: { Action: "IsUserName", UserName: UserName },
  19. cache: false, //设置时候从浏览器中读取 缓存 true 表示 是
  20. datatype: "json",
  21. async: false,
  22. success: function (result) {
  23. if (result.code == 1) {
  24. msg += "该用户名已存在!/r/n";
  25.  
  26. }
  27. }
  28. });
  29. }
  30.  
  31. if (msg == "") {
  32. return true;
  33. } else {
  34. alert(msg);
  35. return false;
  36. }
  37.  
  38. }
  39.  
  40. //总计金额 可以写成js方法
  41. function BuyTotal() {
  42.  
  43. var Total = $.ajax({
  44. type: "POST",
  45. dataType: text,//xml html script json
  46. url: "/Controller/UserHandler.ashx",
  47. data: { Action: "BuyTotal" },
  48. async: false
  49. }).responseText;
  50.  
  51. return Total;
  52. }
  53.  
  54. </script>

2、返回Model的handler代码

  1. <%@ WebHandler Language="C#" Class="UserHandler" %>
  2.  
  3. using System;
  4. using System.Web;
  5. using ECS.Utility;
  6. using System.Collections.Generic;
  7.  
  8. //使用session的时候必须继承IRequiresSessionState接口:, System.Web.SessionState.IRequiresSessionState
  9. public class UserHandler : IHttpHandler
  10. {
  11.  
  12. public void ProcessRequest(HttpContext context)
  13. {
  14. context.Response.ContentType = "text/plain";
  15. //全局默认返回信息
  16. string Json = JsonUtil.Serialize(new { code = 0, msg = "" });
  17.  
  18. //取值
  19. string Action = context.Request.Form["Action"] == null ? "" : context.Request.Form["Action"].ToString().Trim();
  20. string UserName = context.Request.Form["UserName"] == null ? "" : context.Request.Form["UserName"].ToString().Trim();
  21. //判断
  22. if (!string.IsNullOrEmpty(Action))
  23. {
  24. //验证用户
  25. if (Action.Equals("IsUserName"))
  26. {
  27. //验证
  28. if (IsExistUser(UserName))
  29. {
  30. Json = JsonUtil.Serialize(new { code = 1, msg = "用户名已存在!" });
  31. }
  32. else
  33. {
  34. Json = JsonUtil.Serialize(new { code = 0, msg = "用户名可以注册!" });
  35. }
  36.  
  37. }
  38.  
  39. //计算金额
  40. if (Action.Equals("BuyTotal"))
  41. {
  42. //Json格式
  43. //Json = JsonUtil.Serialize(new { code = 1, msg = "成功", Total = 100 });
  44. //
  45. Json = "100";
  46. }
  47. else
  48. { //Json格式
  49. //Json = JsonUtil.Serialize(new { code = 0, msg = "失败", Total = 0 });
  50. Json = "0";
  51. }
  52.  
  53. }
  54.  
  55. //最终返回信息
  56. context.Response.Write(Json);
  57. }
  58.  
  59. /// <summary>
  60. /// 判断UserName是否存在
  61. /// </summary>
  62. /// <param name="userName">用户名</param>
  63. /// <returns>返回true or false</returns>
  64. public bool IsExistUser(string userName)
  65. {
  66.  
  67. List<LSY.Model.A_User> UserList = new LSY.BLL.A_User().GetList(null, "UserName='" + userName.Trim() + "'and Type=0", null);
  68. if (UserList.Count > 0)
  69. {
  70. return true;
  71. }
  72. else
  73. {
  74. return false;
  75. }
  76. }
  77.  
  78. public bool IsReusable
  79. {
  80. get
  81. {
  82. return false;
  83. }
  84. }
  85.  
  86. }

3、页面script代码-【DataTable、List】

  1. //查询城市
  2. function getCity(CityVal) {
  3. var DDL_Province = $("#DDL_Province");
  4. var DDL_City = $("#DDL_City");
  5. DDL_City.empty();
  6. DDL_City.append("<option value=\"0\">市/区</option>");
  7. $.ajax(
  8. {
  9. type: "post",
  10. url: "/UserCart/Controller/CityAreas.ashx",
  11. data: { "type": "city", "provinceID": DDL_Province.val() },
  12. dataType: "json",
  13. async: false,
  14. success: function (msg) {
  15.  
  16. for (var i = 0; i < msg.length; i++) {
  17.  
  18. if (CityVal == msg[i].CityName) {
  19. if (msg[i].IsCOD == 1) {
  20. DDL_City.append("<option value=" + msg[i].CityID + " selected=\"selected\">" + msg[i].CityName + "*</option>");
  21. } else {
  22. DDL_City.append("<option value=" + msg[i].CityID + " selected=\"selected\">" + msg[i].CityName + "</option>");
  23. }
  24. } else {
  25. if (msg[i].IsCOD == 1) {
  26. DDL_City.append("<option value=" + msg[i].CityID + " >" + msg[i].CityName + "*</option>");
  27. } else {
  28. DDL_City.append("<option value=" + msg[i].CityID + " >" + msg[i].CityName + "</option>");
  29. }
  30. }
  31. }
  32. getArea('');
  33. GetAddreesSpan();
  34. }
  35. })
  36. };

4、返回数据集Handler代码

  1. <%@ WebHandler Language="C#" Class="CityAreas" %>
  2.  
  3. using System;
  4. using System.Web;
  5. using System.Collections.Generic;
  6.  
  7. public class CityAreas : IHttpHandler
  8. {
  9.  
  10. public void ProcessRequest(HttpContext context)
  11. {
  12. context.Response.ContentType = "text/plain";
  13. if (context.Request["type"] == "city")
  14. {
  15. context.Response.Write(select2(context.Request["provinceID"]));
  16. }
  17. else if (context.Request["type"] == "district")
  18. {
  19. context.Response.Write(select3(context.Request["cityID"]));
  20. }
  21. }
  22.  
  23. public string select2(string id)
  24. {
  25. string str = string.Empty;
  26. if (!string.IsNullOrEmpty(id))
  27. {
  28. List<ECS.Model.A_CityAreas> list = new ECS.BLL.A_CityAreas().GetList(null, "deep=2 and ParentID=" + id, null);
  29. if (list != null && list.Count > 0)
  30. {
  31.  
  32. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  33. sb.Append("[");
  34. foreach (ECS.Model.A_CityAreas item in list)
  35. {
  36. sb.Append("{");
  37. sb.Append("\"CityID\":\"" + item.id + "\",\"CityName\":\"" + item.AreaName + "\",\"IsCOD\":\"" + item.IsCOD + "\"");
  38. sb.Append("},");
  39. }
  40. sb.Remove(sb.Length - 1, 1);
  41. sb.Append("]");
  42. str = sb.ToString();
  43. }
  44.  
  45. }
  46. return str;
  47. }
  48.  
  49. public string select3(string id)
  50. {
  51. string str = string.Empty;
  52. if (!string.IsNullOrEmpty(id))
  53. {
  54. List<ECS.Model.A_CityAreas> list = new ECS.BLL.A_CityAreas().GetList(null, "deep=3 and ParentID=" + id, null);
  55. if (list != null && list.Count > 0)
  56. {
  57.  
  58. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  59. sb.Append("[");
  60. foreach (ECS.Model.A_CityAreas item in list)
  61. {
  62. sb.Append("{");
  63. sb.Append("\"DistrictID\":\"" + item.id + "\",\"DistrictName\":\"" + item.AreaName + "\",\"IsCOD\":\"" + item.IsCOD + "\"");
  64. sb.Append("},");
  65. }
  66. sb.Remove(sb.Length - 1, 1);
  67. sb.Append("]");
  68. str = sb.ToString();
  69. }
  70.  
  71. }
  72. return str;
  73. }
  74. public bool IsReusable
  75. {
  76. get
  77. {
  78. return false;
  79. }
  80. }
  81.  
  82. }

Jquery+Json+Handler文件结合应用实例的更多相关文章

  1. Jquery Uploadify多文件上传实例

    jQuery Uploadify开发使用的语言是java. 详细的相关文档,可以参考官网的doc:http://www.uploadify.com/documentation/ 官网的讲解还是很详细的 ...

  2. jQuery插件AjaxFileUpload文件上传实现Javascript多文件上传功能

     Ajax file upload plugin是一个功能强大的文件上传jQuery插件,可自定义链接.或其它元素庖代传统的file表单上传结果,可实现Ajax动态提示文件上传 过程,同时支撑多文 ...

  3. JQuery 获取json数据$.getJSON方法的实例代码

    这篇文章介绍了JQuery 获取json数据$.getJSON方法的实例代码,有需要的朋友可以参考一下 前台: function SelectProject() { var a = new Array ...

  4. Struts2+JQuery+Json登陆实例

    Struts2+JQuery+Json登陆实例 博客分类: Struts2   在搭建之前.. 首先,需要准备struts2.0框架的5个核心包, 以及jsonplugin-0.32.jar 以及js ...

  5. jquery ajax返回json数据进行前后台交互实例

    jquery ajax返回json数据进行前后台交互实例 利用jquery中的ajax提交数据然后由网站后台来根据我们提交的数据返回json格式的数据,下面我来演示一个实例. 先我们看演示代码 代码如 ...

  6. python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码

    python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...

  7. jquery下json数组的操作用法实例

    jquery下json数组的操作用法实例: jquery中操作JSON数组的情况中遍历方法用的比较多,但用添加移除这些好像就不是太多了. 试过json[i].remove(),json.remove( ...

  8. jQuery FileUpload等插件的使用实例

    1.jQuery FileUpload 需要的js: jquery.js jquery.fileupload.js jquery.iframe-transport.js jquery.xdr-tran ...

  9. JS jQuery json日期格式问题的办法

    原生JS:Date对象详细参考 Date对象:基于1970年1月1日(世界标准时间)起的毫秒数 本文参考MDN做的详细整理,方便大家参考MDN 构造函数: new Date(); 依据系统设置的当前时 ...

随机推荐

  1. iOS-状态栏字体颜色【白色】【Xcode9.1】

    Xcode9之前 设置状态栏颜色首先在info.plist文件中,加入UIViewControllerBasedStatusBarAppearance = false: <key>UIVi ...

  2. C# 实现Html转JSON

    Html为树结构->Json为数组结构 应用场景 H5或浏览器展示Html代码没有问题,但是让原生APP或ReactNative直接展示Html可能会有很多不便 实现方法 可以通过正则表达式捕获 ...

  3. Spring对远程服务的支持

    Java程序有以下的远程调用技术选择: 远程过程调用(RPC)是同步的,客户端在服务器端返回结果之前将一直被阻塞. 各种技术适用的场景如下: 典型的RMI开发的过程如下: 定义一个接口,用于客户端和服 ...

  4. BZOJ 4008: [HNOI2015]亚瑟王 [DP 概率 !!!]

    传送门 题意: $r$轮$n$张卡牌,每一轮依次考虑每张卡牌,$p_i$概率发动造成$d_i$伤害后结束本轮或者继续考虑下一张 每张卡牌发动过之后以后都会跳过 求$r$轮之后的期望伤害 看了一节课出题 ...

  5. C primer Plus_part6

    第十章  数组和指针 1.const :保护变量不受改变,特别是在作为入参传入函数 对于变量:const 不能修改值 对于指针: const 可以修改值,但是不能修改指向对象 #include< ...

  6. 通过四个例子理解JavaScript拓展运算符

    原文地址:JavaScript & The spread operator 拓展运算符看起来像什么? 三个点,... 它能做什么? 拓展运算符允许一个表达式在某个地方展开成为多个元素.变量或参 ...

  7. SparkSteaming运行流程分析以及CheckPoint操作

    本文主要通过源码来了解SparkStreaming程序从任务生成到任务完成整个执行流程以及中间伴随的checkpoint操作 注:下面源码只贴出跟分析内容有关的代码,其他省略 1 分析流程 应用程序入 ...

  8. 微信小程序模板发送,openid获取,以及api.weixin.qq.com不在合法域名内解决方法

    主要内容在标题三,老手可直接跳到标题三. 本文主要解决个人开发者模板消息发送的问题(没有服务器,不能操作服务器的情况) 针对api.weinxin.qq.com不在以下合法域名列表内的问题提出的解决方 ...

  9. MathJax使用指南

    MathJax使用指南 SublimePrettyJson Github CSDN-Markdown语法集锦 LaTex 简明教程 在Markdown中输入数学公式(MathJax) MathJax ...

  10. FTP主动模式和被动模式

    FTP主动模式和被动模式 FTP是仅基于TCP的服务,不支持UDP.与众不同的是FTP使用2个端口,一个数据端口和一个命令端口(也可叫做控制端口).通常来说这两个端口是21(命令端口)和20(数据端口 ...