When a parameter is submitted to the background with $.ajax (), the array in the argument is usually received in the background by list<t>, but it is always unsuccessful as the following code
var arr1 = [{"Name": "Tom", "Age": +}, {"Name": "Jim", "Age": 22}];var arr2 = [{"Name": "Tom2", "Age": +}, {"Name" : "Jim2", "Age": +}]; $ (function () { $.ajax ({ URL: '/home/useradd ', data: {LIST1:ARR1,LIST2:ARR2}, success:function (msg { if (msg = = ' 1 ') { console.log (' Add success '); } else { console.log (' Add failed ');}} );} )
After monitoring with fiddler, the data becomes
List1[0][name]:tom
List1[0][age]:17
List1[1][name]:jim
List1[1][age]:22
List2[0][name]:tom2
List2[0][age]:18
List2[1][name]:jim2
List2[1][age]:24
The array that can be recognized in C # should be in this format
list1[0].aa=1&list1[0].bb=2&list1[1].aa=3&list1[1].bb=4&list2[0].aa=1&list2[0].bb=2& List2[1].aa=3&list2[1].bb=4
After looking at the information on the web and knowing that Ajax post would be used because jquery needs to call Jquery.param serialization parameters, let's look at the jquery source
In the Ajax () method, the JSON-type data is $.param () processed if (S.data && s.processdata && typeof s.data!== "string") { s . data = Jquery.param (S.data, s.traditional);} Param method if (Jquery.isarray (a) | | | (A.jquery &&!jquery.isplainobject (a))) { //Serialize The form elements Jquery.each (A, function () { Add (this.name, this.value); }); } els e { //If Traditional, encode the ' old ' (the 1.3.2 or older //did it), otherwise encode params recursivel Y. For (prefix in a) { buildparams (prefix, a[prefix], traditional, add); } }
I'll do it after I find out why.
First, traditional is false, we can prevent deep serialization by setting traditional to True
First, write an array to the object method:
Array.prototype.serializeObject = function (lName) { var o = {}; $t = this; for (var i = 0; i < $t. length; i++) {for (var item in $t [i]) { o[lname + ' [' + i + ']. ' + item.tostring ()] = $ T[i][item].tostring (); } } return o; };
$.ajax ({ URL: '/home/useradd ', data: $.param (Arr1.serializeobject ("List1")) + "&" + $.param ( Arr2.serializeobject ("List2")), success:function (msg) { if (msg = = ' 1 ') { console.log (' Add success '); } else { Console.log (' Add failed ');}} );
C # background receive code
public string UserAdd (list<user> list1, list<user> list2) {
So the problem is solved!
Problems with using AJAX methods to submit multiple object arrays