Reputation: 6622
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.
Upvotes: 0
Views: 9123
Reputation: 9559
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)
Upvotes: 5
Reputation: 9
Hope this should help for you:
var obj = jQuery.parseJSON( '{ "name": "John" }' ); alert( obj.name === "John" );
Upvotes: -1
Reputation: 3559
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);
},
});
Upvotes: 1
Reputation: 431
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.
Upvotes: 0