title: How to return an object from a Spring MVC controller in response to AJAX request?
body: <p>I have to return a list of employees from a controller in response to jQuery AJAX request. How should I do for it? </p>
  
  <p>My controller:</p>
  
  <pre class="lang-java prettyprint-override"><code>@RequestMapping("phcheck")
  public ModelAndView pay(@RequestParam("empid") int empid, String fdate, String tdate) {
      ModelAndView mav = new ModelAndView("phcheck");
      List&lt;Employee&gt; 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;
  }
  </code></pre>
  
  <p><br>
  AJAX code in the related <em>view</em>:</p>
  
  <pre class="lang-js prettyprint-override"><code>$(document).ready(function () {
      $("#empid").change(function () {
          if ($("#fdate").val() != "" &amp;&amp; $("#tdate").val() != "" &amp;&amp; $("#empid").val() != "") {
              jQuery.ajax({
                  url: "phcheck.htm?empid=" + $("#empid").val() +
                                   "&amp;&amp;fdate=" + $("#fdate").val() +
                                   "&amp;&amp;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");
          }
      });
  });
  </code></pre>
  
  <p><br>
  Thanks in advance.</p>