`

AJAX $.toJSON的用法或把数组转换成json类型

阅读更多

1. html页面全部代码

<html>
<head>
    <title></title>
    <script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="../../Scripts/JqueryJson.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#json").click(function () {

             //数组里的字段的命名和类型要和一般处理程序里定义的类里的变量要一样

             //否则会出问题
                var postdata = new Array();
                postdata[1] = { id: 1, number: "yes" };
                postdata[2] = { id: 2, number: "no" };

 

                var postData = $.toJSON(postdata);  //把数组转换成json字符串

                //将json字符串反序列化,这个只是测试一下数组是否转换成json字符串

                var content = $.parseJSON(postData);
                $.each(content, function () {
                    alert(this.number);
                });

                //post提交并处理

                $.post("json.ashx", { "array": postData }, function (data, status) {
                    if (status == "success") {
                        alert(data);
                    }
                });

            });
        })
    </script>
</head>
<body>
<input type="button" value="json" id="json"/>
</body>
</html>

2.json.ashx页面全部代码

<%@ WebHandler Language="C#" Class="json" %>

using System;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections.Generic;

public class json : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
               
        //接受出过来的值  

        string sun = context.Request["array"].ToString();

        //实例化JavaScriptSerializer对象
        JavaScriptSerializer jss = new JavaScriptSerializer();
        List<array> a = new List<array>();

        //把json转换其他list<array>类型
        a = jss.Deserialize(sun, typeof(List<array>)) as List<array>;
        string meg=null;
        foreach (var item in a)
        {
            meg += item.number;
        }
        context.Response.Write(meg);
    }

    public class array
    {
        public int id { get; set; }
        public string number { get; set; }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}

 

 

我的代码:

function ClickPreViewHandle(obj) {
        var divid = $(obj).attr("surfaceid").replace("btnPV", "div")
        var inputvalues = GetSurfaceTextInputValue(divid);
        var inputvaluesJS = $.toJSON(inputvalues);
        jQuery.blockUI({ message: "Generation Image...", css: { padding: 25, color: '#fff', border: '3px solid #aaa', backgroundColor: '#507691'} });
        $.ajax({
            type: "post",
            url: "/PersonalizerPreViewHandler.ashx",
            //dataType: "json",
            data: { 'values': inputvaluesJS },
            success: function (data) {
                var leftA = divid.replace("div", "divimg");
                $("#" + leftA + " img").attr("src", data);
                jQuery.unblockUI();
            },
            error: function (err) {
                alert("error:" + err);
                jQuery.unblockUI();
            }
        });
        return false;
    }
 Handler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using THY.Allure.BusinessEntities.BaseEntities;

namespace THY.Allure.Web
{
    /// <summary>
    /// Summary description for PersonalizerPreViewHandler
    /// </summary>
    public class PersonalizerPreViewHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string optionBOStr = context.Request.Params["values"];
            JavaScriptSerializer jss = new JavaScriptSerializer();
            List<ProductDetailOptionsBO> optionBOs = new List<ProductDetailOptionsBO>();
            optionBOs = jss.Deserialize(optionBOStr, typeof(List<ProductDetailOptionsBO>)) as List<ProductDetailOptionsBO>;
            
            context.Response.Write("~/media/images/cordial/product-surface-images/Postcards/PC1476/010.ashx");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
 
0
3
分享到:
评论
1 楼 liuweihug 2014-04-08  
Jquery+asp.net 后台数据传到前台js进行解析的办法说明 - 项目实战 - IT工作生活这点事。Just Such So!
http://www.suchso.com/projecteactual/jquery-aspnet-back-js-parse-object-list.html

相关推荐

    JQuery权威指南源代码

    使用$.isPlainObject()函数检测对象是否为原始对象 使用$.contains()函数检测两个节点是否包含 使用$.param()进行数组元素序列化 使用函数$.extend()扩展工具函数 使用函数$.proxy()改变事件函数的作用域 使用...

    jquery-1.1.3 效率提高800%

    // the options for this ajax request }timeout 数据类型: Number 如果通过$.ajaxSetup设置了一个全局timeout,那么此函数使用一个局部timeout覆盖了全局timeout(单位为毫秒)。例如,你可以设置比较长的...

    ZendFramework中文文档

    1. Introduction to Zend Framework 1.1. 概述 1.2. 安装 2. Zend_Acl 2.1. 简介 2.1.1. 关于资源(Resource) 2.1.2. 关于角色(Role) 2.1.3. 创建访问控制列表(ACL) 2.1.4. 注册角色(Role) 2.1.5. 定义访问...

    jQuery权威指南-源代码

    其次详细讲解了jQuery的各种选择器、jQuery操作DOM的方法、jQuery中的事件与应用、jQuery中的动画和特效、Ajax在jQuery中的应用,以及各种常用的jQuery插件的使用方法和技巧,所有这些知识点都配有完整的示例(包括...

    jQuery权威指南366页完整版pdf和源码打包

    9.2.2 数组和对象的操作 9.2.3 字符串操作 9.2.4 测试操作 9.2.5 url操作 9.3 工具函数的扩展 9.4 其他工具函数—$.proxy() 9.5 综合案例分析—使用jquery扩展工具函数实现对字符串指定类型的检测 ...

    Java学习笔记-个人整理的

    {4.14}数组转换为集合}{89}{section.4.14} {4.15}Map的迭代}{90}{section.4.15} {4.15.1}字符统计}{91}{subsection.4.15.1} {5}格式化输入输出}{94}{chapter.5} {5.1}时间与日期}{94}{section.5.1} {5.1.1}...

    Visual.Basic.2010.&.NET4.高级编程(第6版)-文字版.pdf

    2.2 使用visual basic类型 68 2.2.1 值类型和引用类型 69 2.2.2 基本类型 71 2.3 命令:条件语句 72 2.3.1 if then 73 2.3.2 比较运算符 73 2.3.3 select case 75 2.4 值类型(结构) 75 2.4.1 布尔...

    play框架手册

    支持类型的数组或集合 - 28 - POJO对象绑定 - 29 - JPA 对象绑定 - 30 - 定制绑定 - 30 - @play.data.binding.As - 30 - @play.data.binding.NoBinding - 31 - play.data.binding.TypeBinder - 31 - @play.data....

Global site tag (gtag.js) - Google Analytics