I need this list of employees in ajax
In spring, when you need object serialization, de-serialization and message conversion. in this case, you need to annotate the controller handler method using @RequestBody and @ResponseBody .
Where:
- @ResponseBody: tells spring to try to convert its return value and automatically write it to the http response.
- @RequestBody: tells spring, which is trying to convert the contents of the body of the incoming request into a parameter object on the fly.
in your case, you need a JSON type, you must add @ResponseBody to your method signature or just above the method, and also produce and consume, which are optional, for example:
@RequestMapping(value="phcheck", method=RequestMethod.GET produces="application/json") public @ResponseBody List<Employee> pay(@RequestParam("empid") int empid, String fdate, String tdate) {
and in an AJAX call use:
- Attribute
contentType: 'application/json' indicates the type of data you are sending. and attributedataType: json tells jquery what type of response will be received.
in your case contentType: 'application/json' not required, by default one ie 'application/x-www-form-urlencoded; charset=UTF-8' 'application/x-www-form-urlencoded; charset=UTF-8' enough.
and you can get a list of employees in your AJAX success, iterate over it:
success: function (data) { $.each(data, function(index, currEmp) { console.log(currEmp.name);
<h / "> Note: Jackson mapper or any other mapper must be available along the build path in order to work with JSON serialization and deserialization.
See also:
Rembo
source share