1

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

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 Commented Mar 13, 2014 at 5:01
  • @Ian Thanx for the reply.Tried that change but values still not getting passed. Commented Mar 13, 2014 at 5:06

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Tried this method but values are not getting passed. Thanx anyways:)
2

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");
        }
    });
}

5 Comments

Why should the OP do that? What is the problem and how does your code solve it? Explain your solution!
Here User want a Solution with [Key,Value] pair Array, and in my given solution the pair is made on the basis of: [height,weight] which is passed to the Server side in the array[key,value] format. @FelixKling Just have a look on my Edited code.
You should edit your question and add the explanation.
@FelixKling Just go through the solution that I have Provided. If the guy asking question won't understand then he/she should ask for explanations..
@ Rahul RJ edited my code to add this line var vitals[]; vitals.push({ Height: 170 ,Weight:55}); when I check the value in the generateCCD function with breakpoint it shows the value of ccdEntity an {object} and does not display its contents. Any thoughts?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.