zoukankan      html  css  js  c++  java
  • js 和C# ashx之间数组参数传递问题

    js在进行ajax提交时,如果提交的参数是数组,js无法直接进行提交,及时提交上去,解析也是比较麻烦

    ajax在提交数组时,需要设置参数:

     traditional: true,  //参数作为数组传递时
    
    另外,数组需要进行json.stringy变成字符串进行提交
    完整的格式:
    $.ajax({
            type: "post",
            url: "../Aspose/AsposeHelper.ashx",
            data: { "action": "exportQuery", "queryResult": JSON.stringify( datagridSource )},
            traditional: true,  //参数作为数组传递时
            dataType:"json",
            error: function (ex) {
                console.log("导出查询结果出错:" + ex);
            },
            success: function (data) {
                if (data != null) {
                    debugger;
                     window.open(data["responseObject"]);
                }
            }
        });

    js提交数组后,C#在ashx进行数据接收和处理时,
    【1】对数组参数进行重组
    具体的重组过程:
    1.建立一个与json数组中对象字段一致的类
    2.使用序列化,将js提交上来的数组json字符串反序列化为对象
     /// <summary>
            /// Json格式数据转换为List<T>
            /// </summary>
            public static List<T> JSONStringToList<T>(string JsonStr)
            {
                JavaScriptSerializer Serializer = new JavaScriptSerializer();
                //设置转化JSON格式时字段长度
                List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
                return objs;
            }
    3.按照处理C#中的数组或者list进行处理
  • 相关阅读:
    加入mapstruct后出现 找不到符号 符号: 方法 setXX 的解决方法
    解决docker容器日志导致主机磁盘空间满了的情况
    prometheus安装(docker)
    在Github或Gitee上用hexo搭建个人博客
    解决github打不开
    jenkins更新为国内源
    让sentinel-dashboard的流控配置持久化到nacos
    Yarn和Zookeeper的区别
    flink安装启动(docker)
    jQuery 事件源码定位
  • 原文地址:https://www.cnblogs.com/sguozeng/p/9230427.html
Copyright © 2011-2022 走看看