0

This is my code on the client side:

 $.ajax({
                    type:'POST',
                    charset:'utf-8',
                    url:'http://localhost:8180/GisProject/MainService',
                    data:JSON.stringify(params),
                    success:function(msg)
                    {
                        console.log(msg);
                    },
                    error:function(xhr,status)
                    {
                        console.log(status);
                    },      
                    contentType:"application/json"  
            });

I have previously parsed this data in Node using express.bodyParser but now I have to parse it using the servlet.I have seen people assign variables here without using JSON.stringify and getting that variable using request.getParameter(myData).

What is the standard way of getting the JSON data into the servlet?

And why do people seem to be sending Javascript objects with JSON embedded as a String within like data:{mydata:JSON.stringify(actualData)}?

In case I was not being clear,I want to use the doPost method's request object to get the data I sent from the client side.

vamsiampolu
  • 6,328
  • 19
  • 82
  • 183

4 Answers4

5

On the server side in a servlet you can read POST data payload from request.getReader()

And you can use JSON library like GSON to parse JSON. Something like:

YourClass obj = new Gson().fromJson(request.getReader(), YourClass.class)
kostya
  • 9,221
  • 1
  • 29
  • 36
1

Try this:

 $.ajax({
            type:"POST",
            url:'http://localhost:8180/GisProject/MainService',
            data:{mydata:JSON.stringify(params)},
            datatype:"json",
            success:function(msg)
            { 
               console.log(msg);
            },
            error:function(xhr,status)
            {
                 console.log(status);
             }, 
        });
Mr.G
  • 3,413
  • 2
  • 16
  • 20
  • Doesnt this append the myData to the end of the url and send it as a get parameter?Also,isnt dataType for the output I get from the server? – vamsiampolu Apr 04 '14 at 05:38
0

you can send request and response object to doGet method and than get the json in the same way.

Way to send object to doGet

doGet(request, response); call it in to the post method.

user2075328
  • 423
  • 2
  • 7
  • 16
-1

Hope this should help for you:

var obj = jQuery.parseJSON( '{ "name": "John" }' ); alert( obj.name === "John" );