JS实现Ajax,Josn数据的序列化和反序列化---例: 省市区联动(包含get,post)

服务器端相应JOSN数据   用到序列化和反序列化----命名空间using System.Web.Script.Serialization;

 public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //获取请求的参数
            string id = context.Request["id"].ToString();
            //通过参数查询父级id是参数的数据
            DataClasses1DataContext db = new DataClasses1DataContext();
            List<ChinaStates> list = db.ChinaStates.Where(p => p.ParentAreaCode == id).ToList();
            //将查询到的数据序列化,,用到using System.Web.Script.Serialization;
            JavaScriptSerializer jss = new JavaScriptSerializer();
            //调用jss.Serialize(list)方法,,得到josn类型的字符串,使用时要反序列化,得到josn数组
            string josnData = jss.Serialize(list);
            context.Response.Clear();
            context.Response.Write(josnData);
            context.Response.End();
        }

通过以上方法得到的JOSN数据----崩溃类型0.0

[{"AreaCode":"13","AreaName":"河北","ParentAreaCode":"0001","Root":false,"Zone":2,"NavigateURL":null},{"AreaCode":"14","AreaName":"山西","ParentAreaCode":"0001","Root":false,"Zone":2,"NavigateURL":null},{"AreaCode":"15","AreaName":"内蒙古","ParentAreaCode":"0001","Root":false,"Zone":2,"NavigateURL":null},{"AreaCode":"21","AreaName":"辽宁","ParentAreaCode":"0001","Root":false,"Zone":1,"NavigateURL":null},{"AreaCode":"22","AreaName":"吉林","ParentAreaCode":"0001","Root":false,"Zone":1,"NavigateURL":null},{"AreaCode":"23","AreaName":"黑龙江","ParentAreaCode":"0001","Root":false,"Zone":1,"NavigateURL":null},{"AreaCode":"31","AreaName":"上海","ParentAreaCode":"0001","Root":false,"Zone":7,"NavigateURL":null},{"AreaCode":"32","AreaName":"江苏","ParentAreaCode":"0001","Root":false,"Zone":7,"NavigateURL":null},{"AreaCode":"33","AreaName":"浙江","ParentAreaCode":"0001","Root":false,"Zone":7,"NavigateURL":null},{"AreaCode":"34","AreaName":"安徽","ParentAreaCode":"0001","Root":false,"Zone":7,"NavigateURL":null},{"AreaCode":"35","AreaName":"福建","ParentAreaCode":"0001","Root":false,"Zone":7,"NavigateURL":null},{"AreaCode":"36","AreaName":"江西","ParentAreaCode":"0001","Root":false,"Zone":4,"NavigateURL":null},{"AreaCode":"37","AreaName":"山东","ParentAreaCode":"0001","Root":false,"Zone":7,"NavigateURL":null},{"AreaCode":"41","AreaName":"河南","ParentAreaCode":"0001","Root":false,"Zone":4,"NavigateURL":null},{"AreaCode":"42","AreaName":"湖北","ParentAreaCode":"0001","Root":false,"Zone":4,"NavigateURL":null},{"AreaCode":"43","AreaName":"湖南","ParentAreaCode":"0001","Root":false,"Zone":4,"NavigateURL":null},{"AreaCode":"44","AreaName":"广东","ParentAreaCode":"0001","Root":false,"Zone":5,"NavigateURL":null},{"AreaCode":"45","AreaName":"广西","ParentAreaCode":"0001","Root":false,"Zone":6,"NavigateURL":null},{"AreaCode":"46","AreaName":"海南","ParentAreaCode":"0001","Root":false,"Zone":5,"NavigateURL":null},{"AreaCode":"50","AreaName":"重庆","ParentAreaCode":"0001","Root":false,"Zone":6,"NavigateURL":null},{"AreaCode":"51","AreaName":"四川","ParentAreaCode":"0001","Root":false,"Zone":6,"NavigateURL":null},{"AreaCode":"52","AreaName":"贵州","ParentAreaCode":"0001","Root":false,"Zone":6,"NavigateURL":null},{"AreaCode":"53","AreaName":"云南","ParentAreaCode":"0001","Root":false,"Zone":6,"NavigateURL":null},{"AreaCode":"54","AreaName":"西藏","ParentAreaCode":"0001","Root":false,"Zone":3,"NavigateURL":null},{"AreaCode":"61","AreaName":"陕西","ParentAreaCode":"0001","Root":false,"Zone":3,"NavigateURL":null},{"AreaCode":"62","AreaName":"甘肃","ParentAreaCode":"0001","Root":false,"Zone":3,"NavigateURL":null},{"AreaCode":"63","AreaName":"青海","ParentAreaCode":"0001","Root":false,"Zone":3,"NavigateURL":null},{"AreaCode":"64","AreaName":"宁夏","ParentAreaCode":"0001","Root":false,"Zone":3,"NavigateURL":null},{"AreaCode":"65","AreaName":"新疆","ParentAreaCode":"0001","Root":false,"Zone":3,"NavigateURL":null},{"AreaCode":"71","AreaName":"台湾","ParentAreaCode":"0001","Root":false,"Zone":0,"NavigateURL":null},{"AreaCode":"81","AreaName":"香港","ParentAreaCode":"0001","Root":false,"Zone":0,"NavigateURL":null},{"AreaCode":"91","AreaName":"澳门","ParentAreaCode":"0001","Root":false,"Zone":0,"NavigateURL":null}]

JosnViewer---神兵利器!

JS实现Ajax代码

window.onload = function () {
            LoadProv();
            LoadCity();
            LoadTown();

        };//onload
 function LoadProv() {
            //确认事件
            //1.创建xhr对象
            var xhr = null;
            if (XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            } else if (ActiveXObject) {
                xhr = new ActiveXObject("Microsoft.XMLHttp");
            }
            //2.设置回调函数
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    //将响应的数据反序列化
                    var josnObj = eval(‘(‘ + xhr.responseText + ‘)‘);
                    for (var i = 0; i < josnObj.length; i++) {
                        var opt = document.createElement(‘option‘);
                        opt.value = josnObj[i].AreaCode;
                        opt.innerHTML = josnObj[i].AreaName;
                        document.getElementById(‘ddlProv‘).appendChild(opt);
                    }//for
                    document.getElementById(‘ddlProv‘).onchange();//内部调用自己的onchange事件,加载下一个下拉
                }//if
            }
            //3.初始化xhr,三个参数,,提交方式,url,是否异步
            xhr.open(‘get‘, ‘Select.ashx?id=0001‘, true);
            //4.发送请求
            xhr.send(null);
        }
 function LoadCity() {
            //确认事件--改变事件
            document.getElementById(‘ddlProv‘).onchange = function () {
                //获取被选省份的id
                var id = this.value;
                //根据被选择的id加载市区
                var xhr = null;
                //1.创建xhr对象
                if (XMLHttpRequest) {
                    xhr = new XMLHttpRequest();
                } else if (ActiveXObject) {
                    xhr = new ActiveXObject("Microsoft.Http");
                }
                //2.设置回调函数
                xhr.onreadystatechange = function () {
                    //反序列化
                    var josnObj = eval(xhr.responseText);
                    document.getElementById(‘ddlCity‘).innerHTML = ‘‘;
                    if (xhr.readyState = 4 && xhr.status == 200) {
                        for (var i = 0; i < josnObj.length; i++) {
                            var opt = document.createElement(‘option‘);
                            opt.value = josnObj[i].AreaCode;
                            opt.text = josnObj[i].AreaName;
                            document.getElementById(‘ddlCity‘).appendChild(opt);
                        }
                    }
                    document.getElementById(‘ddlCity‘).onchange();
                }
                //3.初始化
                xhr.open(‘post‘, ‘Select.ashx‘, true);
                xhr.setRequestHeader(‘content-type‘, ‘application/x-www-form-urlencoded‘);
                //4.发送
                xhr.send(‘id=‘ + id);
            };//onchange
        }
function LoadTown() {
            //确认事件
            document.getElementById(‘ddlCity‘).onchange = function () {
                var id = this.value;
                var xhr = null;
                //1.创建对象
                if (XMLHttpRequest) {
                    xhr = new XMLHttpRequest();
                } else if (ActiveXObject) {
                    xhr = new ActiveXObject("Microsoft.Http");
                }
                //2.设置回掉函数
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        //反序列化响应,转化为josn格式
                        document.getElementById(‘ddlTown‘).innerHTML = ‘‘;
                        var josnObj = eval(xhr.responseText);
                        for (var i = 0; i < josnObj.length; i++) {
                            var opt = document.createElement(‘option‘);
                            opt.value = josnObj[i].AreaCode;
                            opt.text = josnObj[i].AreaName;
                            document.getElementById(‘ddlTown‘).appendChild(opt);
                        }
                    }
                }
                //3.初始化
                xhr.open(‘post‘, ‘SelectByPost.ashx‘, true);
                //post 必备
                xhr.setRequestHeader(‘content-type‘, ‘application/x-www-form-urlencoded‘);
                //4.发送
                xhr.send(‘id=‘ + id);
            };//onchange
<body>
    <form id="form1" runat="server">
        省:<asp:DropDownList ID="ddlProv" runat="server"></asp:DropDownList>
        市:<asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList>
        区/县:<asp:DropDownList ID="ddlTown" runat="server"></asp:DropDownList>
    </form>
</body>
时间: 2025-01-14 22:13:35

JS实现Ajax,Josn数据的序列化和反序列化---例: 省市区联动(包含get,post)的相关文章

Json数据的序列化与反序列化的三种常用方法介绍

以下内容是本作者从官网中看相应的教程后所做的demo,其体现了作者对相关知识点的个人理解..作者才疏学浅,难免会有理解不到位的地方..还请各位读者批判性对待... 本文主要介绍在Json数据的序列化与反序列化的过程中我经常用到的三种工具的基本使用方法,及其他们之间 的比较...希望能够对读者们有一些帮助... 这里介绍到的三种解析与生成Json数据的方法分别是:标准JSON解析与生成方法.使用gson解析与生成JSON数据 .使用fastJsson解析与生成JSON数据. 首先,这些工具都可以到

UDP畅聊系统数据的序列化与反序列化

comm中实现基本数据的序列化与反序列化 基于jsoncpp库,由c++编写,用于为网络数据提供序列化及反序列化功能 使用的几个类 Json::Value 可表示所有的类型,int,float,string等 Json::Reader 将json字符串解析到Value中,反序列化,使用Parse函数 Json::Writer 将Value转换成json字符串型,序列化 它的两个子类:Json::FastWriter 和Json::StyledWriter 两种不同的显示风格 Makefile  

js实现对json数据的序列化(兼容ie6以上浏览器)

/** * 增加对JSON数据的序列化方法, * 主要用于IE6.7不支持JSON对象的浏览器 */ var xue = xue || {};xue.json = xue.json || {}; xue.json.stringify = function(obj) {    //如果是IE8+ 浏览器(ff,chrome,safari都支持JSON对象),使用JSON.stringify()来序列化    if (window.JSON) {        return JSON.stringi

Vue.js&mdash;&mdash;基于$.ajax实现数据的跨域增删查改

概述 之前我们学习了Vue.js的一些基础知识,以及如何开发一个组件,然而那些示例的数据都是local的.在实际的应用中,几乎90%的数据是来源于服务端的,前端和服务端之间的数据交互一般是通过ajax请求来完成的. 说起ajax请求,大家第一时间会想到jQuery.除了拥有强大的DOM处理能力,jQuery提供了较丰富的ajax处理方法,它不仅支持基于XMLHttpRequest的ajax请求,也能处理跨域的JSONP请求. 之前有读者问我,Vue.js能结合其他库一起用吗?答案当然是肯定的,V

Python 中数据的序列化和反序列化(json处理)

概念: JSON(JavaScript Object Notation):是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等). 这些

C# 编写通用的JSON数据进行序列化和反序列化

注意事项:使用JSON系列化和反系列化,必须要添加引用System.Runtime.Serialization. 1.通用类代码如下: /// <summary>    /// JSON序列化和反序列化辅助类    /// </summary>    public class JsonHelper    {        /// <summary>        /// JSON序列化        /// </summary>        /// <

js中ajax请求数据之后的click事件失效问题

// 初始化数据的时候 $('.shoucang .img-1').click(function(){ // code... }); // ajax加载后click事件不执行,又改成这样 $('.shoucang .img-1').live("click",function(){ // code... }); //jQuery1.9之后由于live被删除了,所以应该这样写: $(document).on("focus","a",function(

js 获取 ajax返回数据及处理

$.ajax({ url: "http://xiaocui.dgoods.cn/app/index.php?i=5&c=entry&do=check&m=stonefish_bigwheel", type: "POST", data:{'mdcode':param}, dataType:"json", async: true, success: function (succ) { var num = succ[0].sca

fastjson生成和解析json数据,序列化和反序列化数据

本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<Map<String,Object>) 2.通过一个android程序测试fastjson的用法. fastjson简介: Fastjson是一个Java语言编写的高性能功能完善的JSON库.fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jack