The problem of submitting multiple object arrays using AJAX methods in JQ

Source: Internet
Author: User
Tags arrays serialization

When using $.ajax () to submit parameters to the background, if the parameters of the array in general in the background will be used to receive list<t>; but always unsuccessful as the following code

var arr1 = [{  "name":  "Tom",  "Age": 17 }, {  "name":  "
Jim ", " Age ": 22}]; var arr2 = [{  "name":  "Tom2",  "Age": 18 }, {  "name":  "

Jim2 ", " Age ": 24}];
    $ (function  ()  {        $.ajax ({             url:  '/home/useradd ',    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;DATA:&NBSP;{LIST1:ARR1,LIST2:ARR2},              success: function  (msg)  {                 if  (msg ==   ' 1 ')  {               
     console.log (' Add success ');                 } else {                     
Console.log (' Add failed ');                 }              }         }

);     })

After using the Fiddler Monitor, we found that the data became

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 such a 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 finding the data on the Internet, I learned that Ajax post would be used because jquery needs to invoke the Jquery.param serialization parameters, we look at the jquery source

In the Ajax () method, the data for the JSON type 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 );

        });     } else {        // If  traditional, encode the  "old"  way  (the way 1.3.2 or  older         // did it), otherwise encode 
Params recursively.         for  ( prefix in a )  {             buildparams ( prefix, a[ 
prefix ], traditional, add );         }     }

I'll do it after I find out why

First, traditional to false, we can prevent depth serialization by setting traditional to True

First write an array to object:

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)
{return
"1";
}
public class User
{public
string Name {get; set;}
public int Age {get; set;}
}

So the problem is solved!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.