tags:

views:

23

answers:

1

Hi I am trying to post an array of javascript objects back to the server, using dojo.

I have an array of data that is like appears like this:

var elements = [
  myobj = {id:"1", name:"myname", title:"mytitle"},
  myobj = {id:"2", name:"myname2", title:"mytitle2"}
] 

I want to post this as a json string back to the server to be handled by a PHP script. However dojo.toJson will only seralise an individual object.

How can I convert the entire array of objects into a json string that can be posted to the backend for processing?

many thanks

+2  A: 

You are misinformed: dojo.toJson() handles arrays without a problem. I just plugged in your elements and that's what I've got:

[{"id":"1","name":"myname","title":"mytitle"},{"id":"2","name":"myname2","title":"mytitle2"}]

Looks okay to me.

But maybe I misunderstood something? Please post a sample proving your point.

Eugene Lazutkin
No you're right... Where I was getting my information was from http://dojotoolkit.org/reference-guide/dojo/toJson.html#dojo-tojson and it only gives you an object reference. After trying again all seems well.Thanks
Grant Collins
Right. An array is an object too. Not all objects are that lucky: some objects like regular expressions, or dates are not supported by JSON directly. But an array is a fundamental part of JSON, so all libraries I know of handle arrays well.
Eugene Lazutkin