I have a restful web service which written in java. And I call it from javascript. I pass a string parameter on it (param=5). But I need to send an object to pass it. How can I pass an object in restful webservice from javascript side? And how can I parse it in java side?
java code :
@RequestMapping(value = "/services/getVillages")
@ResponseBody
public Village getAllVillages(HttpServletRequest request) throws JSONException {
String param = request.getParameter("param");
System.out.println("parametre: " + param );
long startTime = System.currentTimeMillis();
//Village result = innerService.getAllVillagesCacheable(request);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("süre: " + elapsedTime);
startTime = System.currentTimeMillis();
//Village result2 = innerService.getAllVillages(new HttpServletRequest());
stopTime = System.currentTimeMillis();
elapsedTime = stopTime - startTime;
System.out.println("süre cache'siz: " + elapsedTime);
return new Village(); }
javascript code :
function callWS()
{
$.ajax({
type: 'GET',
url: 'http://localhost/services/getVillages/',
data: "param=5", // the data in form-encoded format, ie as it would appear on a querystring
dataType: "json", // the data type we want back, so text. The data will come wrapped in xml
success: function (data) {
alert("party hard"); // show the string that was returned, this will be the data inside the xml wrapper
},
error: function (data) {
alert("restful cagirmada hata"); // show the string that was returned, this will be the data inside the xml wrapper
}
});
};