admin管理员组

文章数量:1410682

I have an key value pair array which I need to send to another function through ajax. My array looks something like this

       var vitals=new Array();
       var vitals["height"]=170; 
       var vitals["weight"]=55; 

the ajax function is

     $.ajax({
    type: "POST", //GET or POST or PUT or DELETE verb
    url: url, // Location of the service
    data: JSON.stringify({ccdEntity: vitals }), //Data sent to server
    contentType: "application/json; charset=utf-8", // content type sent to server
    dataType: "json", //Expected data format from server
    processdata: true, //True or False
    success: function (msg) {//On Successfull service call
        ServiceSucceeded(msg);
    }

and the function receiving the value is

  public bool GenerateCcd( Array ccdEntity)

when I run the program the function does not display the array with key-values but when I transfer a normal array (i.e) vitals[0]=170, it works fine . Do I need to make an changes for sending the above data to function?

Edit: Tried passing the above array as a JSON object

             var vitals= {
        "height": "170",
        "weight": "55"}

but results still the same

I have an key value pair array which I need to send to another function through ajax. My array looks something like this

       var vitals=new Array();
       var vitals["height"]=170; 
       var vitals["weight"]=55; 

the ajax function is

     $.ajax({
    type: "POST", //GET or POST or PUT or DELETE verb
    url: url, // Location of the service
    data: JSON.stringify({ccdEntity: vitals }), //Data sent to server
    contentType: "application/json; charset=utf-8", // content type sent to server
    dataType: "json", //Expected data format from server
    processdata: true, //True or False
    success: function (msg) {//On Successfull service call
        ServiceSucceeded(msg);
    }

and the function receiving the value is

  public bool GenerateCcd( Array ccdEntity)

when I run the program the function does not display the array with key-values but when I transfer a normal array (i.e) vitals[0]=170, it works fine . Do I need to make an changes for sending the above data to function?

Edit: Tried passing the above array as a JSON object

             var vitals= {
        "height": "170",
        "weight": "55"}

but results still the same

Share Improve this question edited Mar 13, 2014 at 5:16 Rohit Ranjit asked Mar 13, 2014 at 4:59 Rohit RanjitRohit Ranjit 171 silver badge6 bronze badges 2
  • 1 Change the vitals declaration to be var vitals = {};. That way you can correctly use a key/value pair structure. Using an array, you were setting properties that aren't serialized. An object ({}) is really what you're looking for – Ian Commented Mar 13, 2014 at 5:01
  • @Ian Thanx for the reply.Tried that change but values still not getting passed. – Rohit Ranjit Commented Mar 13, 2014 at 5:06
Add a ment  | 

2 Answers 2

Reset to default 2

Make your vitals an object array rather than an array;;

   var vitals={'height': '170', 'weight': '55'};

And post your data like:

data: JSON.stringify(vitals)

Use something like this::

function TestAjax() {
    var vitals= [];

    for (var i = 0; i < 5; i++) {
        vitals.push({ Height: (170+i), Weight: (55+i) });
    }

    $.ajax({
        type: 'POST',
        url: url,
        contentType: "application/json",
        data:JSON.stringify( {vitals: vitals}),
        success: function (data) {
            alert("Succeded");
        }
    });
}

本文标签: javascriptPassing KeyValue Array AJAXStack Overflow