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进行处理
  • 相关阅读:
    sklearn中禁止输出ConvergenceWarning:
    sklearn里训练集和测试集的分割
    sklearn模型的保存与加载使用
    django项目成功启动,但views里代码未执行
    使用sklearn对iris数据集做基本的训练和预测
    unrecognized option '--high-entropy-va'
    快速下载Mingw(不使用sourceforge.net)
    cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
    GoLand里Go Module模式下import自定义包
    GoLand生成可执行文件(Windows、Linux)
  • 原文地址:https://www.cnblogs.com/sguozeng/p/9230427.html
Copyright © 2011-2022 走看看