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;
- }
- }
- }
Jquery+Json+Handler文件结合应用实例的更多相关文章
- Jquery Uploadify多文件上传实例
jQuery Uploadify开发使用的语言是java. 详细的相关文档,可以参考官网的doc:http://www.uploadify.com/documentation/ 官网的讲解还是很详细的 ...
- jQuery插件AjaxFileUpload文件上传实现Javascript多文件上传功能
Ajax file upload plugin是一个功能强大的文件上传jQuery插件,可自定义链接.或其它元素庖代传统的file表单上传结果,可实现Ajax动态提示文件上传 过程,同时支撑多文 ...
- JQuery 获取json数据$.getJSON方法的实例代码
这篇文章介绍了JQuery 获取json数据$.getJSON方法的实例代码,有需要的朋友可以参考一下 前台: function SelectProject() { var a = new Array ...
- Struts2+JQuery+Json登陆实例
Struts2+JQuery+Json登陆实例 博客分类: Struts2 在搭建之前.. 首先,需要准备struts2.0框架的5个核心包, 以及jsonplugin-0.32.jar 以及js ...
- jquery ajax返回json数据进行前后台交互实例
jquery ajax返回json数据进行前后台交互实例 利用jquery中的ajax提交数据然后由网站后台来根据我们提交的数据返回json格式的数据,下面我来演示一个实例. 先我们看演示代码 代码如 ...
- python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码
python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...
- jquery下json数组的操作用法实例
jquery下json数组的操作用法实例: jquery中操作JSON数组的情况中遍历方法用的比较多,但用添加移除这些好像就不是太多了. 试过json[i].remove(),json.remove( ...
- jQuery FileUpload等插件的使用实例
1.jQuery FileUpload 需要的js: jquery.js jquery.fileupload.js jquery.iframe-transport.js jquery.xdr-tran ...
- JS jQuery json日期格式问题的办法
原生JS:Date对象详细参考 Date对象:基于1970年1月1日(世界标准时间)起的毫秒数 本文参考MDN做的详细整理,方便大家参考MDN 构造函数: new Date(); 依据系统设置的当前时 ...
随机推荐
- iOS-状态栏字体颜色【白色】【Xcode9.1】
Xcode9之前 设置状态栏颜色首先在info.plist文件中,加入UIViewControllerBasedStatusBarAppearance = false: <key>UIVi ...
- C# 实现Html转JSON
Html为树结构->Json为数组结构 应用场景 H5或浏览器展示Html代码没有问题,但是让原生APP或ReactNative直接展示Html可能会有很多不便 实现方法 可以通过正则表达式捕获 ...
- Spring对远程服务的支持
Java程序有以下的远程调用技术选择: 远程过程调用(RPC)是同步的,客户端在服务器端返回结果之前将一直被阻塞. 各种技术适用的场景如下: 典型的RMI开发的过程如下: 定义一个接口,用于客户端和服 ...
- BZOJ 4008: [HNOI2015]亚瑟王 [DP 概率 !!!]
传送门 题意: $r$轮$n$张卡牌,每一轮依次考虑每张卡牌,$p_i$概率发动造成$d_i$伤害后结束本轮或者继续考虑下一张 每张卡牌发动过之后以后都会跳过 求$r$轮之后的期望伤害 看了一节课出题 ...
- C primer Plus_part6
第十章 数组和指针 1.const :保护变量不受改变,特别是在作为入参传入函数 对于变量:const 不能修改值 对于指针: const 可以修改值,但是不能修改指向对象 #include< ...
- 通过四个例子理解JavaScript拓展运算符
原文地址:JavaScript & The spread operator 拓展运算符看起来像什么? 三个点,... 它能做什么? 拓展运算符允许一个表达式在某个地方展开成为多个元素.变量或参 ...
- SparkSteaming运行流程分析以及CheckPoint操作
本文主要通过源码来了解SparkStreaming程序从任务生成到任务完成整个执行流程以及中间伴随的checkpoint操作 注:下面源码只贴出跟分析内容有关的代码,其他省略 1 分析流程 应用程序入 ...
- 微信小程序模板发送,openid获取,以及api.weixin.qq.com不在合法域名内解决方法
主要内容在标题三,老手可直接跳到标题三. 本文主要解决个人开发者模板消息发送的问题(没有服务器,不能操作服务器的情况) 针对api.weinxin.qq.com不在以下合法域名列表内的问题提出的解决方 ...
- MathJax使用指南
MathJax使用指南 SublimePrettyJson Github CSDN-Markdown语法集锦 LaTex 简明教程 在Markdown中输入数学公式(MathJax) MathJax ...
- FTP主动模式和被动模式
FTP主动模式和被动模式 FTP是仅基于TCP的服务,不支持UDP.与众不同的是FTP使用2个端口,一个数据端口和一个命令端口(也可叫做控制端口).通常来说这两个端口是21(命令端口)和20(数据端口 ...