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进行处理