I want to make an ajax get call to server.Till now I always used:
$.get("/FruitResults?fruit="+fruitname+"&color="+colorname,function(data){addToTables(data);},"text");
for sending parameters fruit,color.Now if I have many fruits,their color,price..
{apple:{color:red,price:30},orange:{color:orange,price:10}}
and such a big list of fruits, how shoud i send this to servlet using Ajax call., in what format? and at servlet side also, how should I retrieve the request parameters from the request object?
Http
get
method is not suitable for sending complex data. Therefore, you must usepost
method to send a complex data from client to the server. And you can use JSON format to encode this data. Example code is as follows:Note that, since you used the
post
method, you have to handle this request indoPost
method of the servlet instead ofdoGet
. To retrieve the posted data you must read the input stream of the servlet request as follows:** Link for gson: http://code.google.com/p/google-gson/