I have to return a list of employees from a controller in response to jQuery AJAX request. How should I do for it?
My controller:
@RequestMapping("phcheck")
public ModelAndView pay(@RequestParam("empid") int empid, String fdate, String tdate) {
    ModelAndView mav = new ModelAndView("phcheck");
    List<Employee> employees = entityManager.createQuery(
        "SELECT e FROM Employee e WHERE e.empId = " + empid, Employee.class)
        .getResultList();
    mav.addObject("employees", employees); // I need this list of employee in AJAX
    return mav;
}
AJAX code in the related view:
$(document).ready(function () {
    $("#empid").change(function () {
        if ($("#fdate").val() != "" && $("#tdate").val() != "" && $("#empid").val() != "") {
            jQuery.ajax({
                url: "phcheck.htm?empid=" + $("#empid").val() +
                                 "&&fdate=" + $("#fdate").val() +
                                 "&&tdate=" + $("#tdate").val(),
                success: function (data) {
                    alert(data + "success");
                },
                error: function (data) {
                    alert(data + "error");
                }
            });
        } else {
            alert("Please fill the from date and to date or select the employee id");
            $("#empid .option").attr("selected", "selected");
        }
    });
});
Thanks in advance.
What you need to do is pass a callback function to the somefunction as a parameter. This function will be called when the process is done working (ie, onComplete): somefunction: function(callback){ var result = ""; myAjax = new Ajax.
You can't return "true" until the ajax requests has not finished because it's asynchron as you mentioned. So the function is leaved before the ajax request has finished.
I need this list of employee in ajax
In spring when you need object serialization, de-serialization and message conversion. in that case you need to annotate your controller handler method with @RequestBody and @ResponseBody. 
Where:
in your case you need JSON type, you have to add @ResponseBody to your method signature or just above the method, and produces and consumes which are optional, like:
@RequestMapping(value="phcheck", method=RequestMethod.GET
                produces="application/json")
public @ResponseBody List<Employee> pay(@RequestParam("empid") int empid, String fdate, String tdate) {
  //get your employee list here
  return empList;
}
and in AJAX call use:
contentType: 'application/json' attribute tells the type of data you're sending. anddataType: json attribute tells jquery what content type of response will receive.in your case contentType: 'application/json' is not needed, default one i.e. 'application/x-www-form-urlencoded; charset=UTF-8' is enough.
and you can receive list of employees in your AJAX success, to iterate over it do like:
  success: function (data) {
          $.each(data, function(index, currEmp) {
             console.log(currEmp.name); //to print name of employee
         });    
        },
See Also:
@RequestMapping(value = "phcheck", produces = "application/json")
@ResponseBody
public ModelAndView pay(@RequestParam("empid") int empid, @RequestParam("fdate") String fdate, @RequestParam("tdate") String tdate) {
   return entityManager.createQuery("select e from Employee e where e.empId="+empid, Employee.class).getResultList();
}
url:"phcheck.htm?empid="+$("#empid").val()+"&fdate="+$("#fdate").val()+"&tdate="+$("#tdate").val()
In Spring MVC you will have to have registered MappingJackson2HttpMessageConverter like being done here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With