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;
        }
    }

}

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-UITableviewcell分割线位置

    这几天又遇到要调节列表分割线位置,就想起很久以前刚做时的做法:把自带的分割线隐藏,然后自己加一条UIView,哈哈,不过这一两年不那么干了,把这个方法贴出来: 在 Tableview 的代理方法中,实 ...

  2. GitHub入门之路(1)

    介绍 从本篇文章开始,是一系列介绍GitHub相关内容以及Git的一些基本操作的文章,记录了自己的学习过程. 概要 简单介绍GitHub是什么,Git又是什么. 1.Git是什么 Git是一款分散型的 ...

  3. UOJ Round #15 [构造 | 计数 | 异或哈希 kmp]

    UOJ Round #15 大部分题目没有AC,我只是水一下部分分的题解... 225[UR #15]奥林匹克五子棋 题意:在n*m的棋盘上构造k子棋的平局 题解: 玩一下发现k=1, k=2无解,然 ...

  4. Linux expect自动登录ssh,ftp

    [http://blog.51yip.com/linux/1462.html#] #!/usr/bin/expect -f set ip 192.168.1.201 set password meim ...

  5. JVM自动内存管理-Java内存区域与内存溢出异常

    摘要: JVM内存的划分,导致内存溢出异常的可能区域. 1. JVM运行时内存区域 JVM在执行Java程序的过程中会把它所管理的内存划分为以下几个区域: 1.1 程序计数器 程序计数器是一块较小的内 ...

  6. centos 配置 php 执行shell的权限

    在执行特定的shell命令,如  kill,killall 等需要配置root权限 php脚本运行在apache服务器下 可以看到 httpd 是以 apache 用户执行的 看一下 该用户信息 现在 ...

  7. python中重要的模块--asyncio

    一直对asyncio这个库比较感兴趣,毕竟这是官网也非常推荐的一个实现高并发的一个模块,python也是在python 3.4中引入了协程的概念.也通过这次整理更加深刻理解这个模块的使用 asynci ...

  8. acdrem1083 人民城管爱人民 DP

    思路:d(i, 0)表示从节点i到达大运村的最短路径,d(i, 1)表示从节点i到达大运村的次短路径. 1.最短路:当做DAG处理即可. 2.次短路:假设当前在u点处,下一个节点是v.v到终点的最短路 ...

  9. Springdata mongodb 版本兼容 引起 Error [The 'cursor' option is required, except for aggregate with the explain argument

    在Spring data mongodb 中使用聚合抛出异常 mongodb版本 为 3.6 org.springframework.dao.InvalidDataAccessApiUsageExce ...

  10. docker-compose 完整打包发布, 多服务,多节点SPRING CLOUD ,EUREKA 集群

    这里不再使用 端口映射的方式,因为不同主机上,Feign 根据 docker hostname访问会有问题. 把打包的好jar copy到docker镜像里 有几个服务,就复制几个dockerfile ...