Login Register

Data to Server as Json how ?

Hi

I am using Dojo 1.1/IE6 i would like to know how can i send JSON data from the client to the server using dojo
also i am using a servlet at the server side.

Code Example for both would be really helpful, i would also like to know that can we create a new store programatically and store values into it and send it to server as JSON.

an example would be the user selects a number of fields and i want to keep adding his selection to a store and just POST the store to the server.

Is it possible .

Thanks

Dino

Yes it is possible

What we do in our code is simply build up a javascript object with the data we want to send to the server. You can then call:

var jsonString = dojo.toJson( dataToSave, false );

The second parameter is whether you want the JSON data "pretty" and indented, etc.

You can then set that as part of the post content when calling dojo.xhrPost. (You can use xhrGet, but you might hit the GET limits per parameter easily)

var postContent = {
  myData: jsonString
};

On the server side, we use the org.json libraries to parse the string into a Java object and then get the data we want. In fact, we use JSON for the return values as well. It works out great.

Irv

Thanks

Hi Irv

Thanks for clearing this. I will be sure to use the same :)

one thing which puzzles me is when in the servlet we want to send any json data back to the itemfilereadstore say,

we do and out.println(jsondatastring);

is this correct in the servlet or is their another method

Dino