Problems with using AJAX methods to submit multiple object arrays

Source: Internet
Author: User

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

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.