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進行處理