1

When I try passing array of array to server it gives(on server log)

1 => [object, object], 2=> [object, object]

using ajax data: data

data on console.log shows:

[Object {name="abc", place="us"}, Object {name="pqr", place="jp"}]

actual array is as follow:

data.push({name: "abc", place: "us"})
data.push({name: "pqr", place: "jp"})

I would like output as (on server):

Person1 = {name="abc", place="us"}
Person2 = {name="pqr", place="jp"}

I tried jQuery.Serialize it does not work and trying to convert it JSON but failed on client side.(giving output [ ])

I am not sure, where I am going wrong. Can I directly send data array to server?

Thanks Viral

2 Answers 2

2

JavaScript arrays and objects are not the same thing. Arrays have elements with numeric indexes, while objects have properties with string key names.

You probably want something like this:

var data = {
   Person1 : {name:"abc", place:"us"},
   Person2 : {name:"pqr", place:"jp"}
};

This creates an object (data) which has properties "Person1" and "Person2" that are themselves references to objects with "name" and "place" properties.

Or, the equivalent of the array push method to add your properties to the object is to do this:

// create an empty object
var data = {};

// set properties
data["Person1"] = {name:"abc", place:"us"};
data["Person2"] = {name:"pqr", place:"jp"};

// or use dot notation:
data.Person3 = {name:"xyz", place:"au"};
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for prompt response and nice explanation :). However, I tried that as well but when I pass array to server I get Person1 => [object, object]
Sorry....it gives Person1 => [object Object] (not comma). Also in console.log i get [ Person1 Object {name="abc", place="us"} ].
Well at least you're getting "Person1" instead of "1", so it's a step in the right direction. Can you show a bit more of your code? Please show the code that creates and populates your object/array, and the ajax call that passes it to the server. You've tagged the question with jQuery, so are you using $.ajax()? jQuery can convert your object to JSON automatically as part of the ajax call. How do you parse it on the server-side?
Why are you using the complicated regexes to split up your "abc/us" format strings? E.g., where you said var p_name = value.match(/^(.*[\\\/])/); - the match method returns an array, is that what you intend? You can just say var p_name = value.split("/")[0]; to get the "abc" part as a string...
The reason I did because, sometimes name has "abc/lastname/us" so split("/")[0] won;t work always. What I need is to separate name & location and I know that after last forward slash (/) is always location and rest is name. Maybe you can point efficient way to do that.
|
1

here is an ajax if you like to try, serializing and passing data object as JSON to the server side.

var data = {
    Person1 : { name:'abc', place:'us'},
    Person2 : { name:'pqr', place:'jp'}
}

$.ajax({
    type: 'POST',
    url: 'url',
    data: JSON.stringify(data),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(msg) {
    //success
  }
});

5 Comments

make sure your server side object is setup the same way. Not sure what you are using in server side but if you let me know i can provide code.
thanks zero7....I will try this as well.. However, JSON.stringify() need some json.js pulgin. I tried that as well...but gave up because firebug was detecting too many errors in json.js file.
hmm not aware of json.js errors. Also, you don't need to use stringify if you don't want to. You can just stringify your JSON object yourself.
I guess you where right..It works the way you suggest but on server side...it divides entire string to letter. For example : name =>"abc", it has "113"=>"a", "51"=>"c", etc... I am using ruby on rails
I just need to decode the JSON object...it worked fine...Thanks for your help

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.