如何将一个列表从spring控制器传递给jquery中的ajax

时间:2017-08-21 11:54:03

标签: javascript jquery ajax spring

这是我的Jquery!这里我没有得到成功响应,而是当一个列表从控制器返回到jquery以下时抛出错误响应。请让我知道我哪里出错了。

//ajax method to retrieve the data from controller where controller returns list
function doAjaxPost() {
  // get the form values
  var pid = $('#pid').val();
  $.ajax({
    type: "GET",
    url: "http://localhost:8085/HMS/iochart1.html",
    data: "pid=" + pid,

    success: function (response) {
      alert(response.list4);
      $.each(response, function (index, datec) {
        alert(datec.name); //to print name of employee
      });
    },
    // Even though controller returns the list,but i am not getting the above success response,instead below error method is executed. 
    error: function (e) {
      alert('Error: ' + e);
    }
  });
}

下面是返回列表的控制器。

@RequestMapping(value="/iochart1", method = RequestMethod.GET)
public @ResponseBody List<Iochart> iochart1(@ModelAttribute("s") Iochart s) {
  System.out.println("Patient"+s.getPid());
  List<Iochart> list4 = dao.getPatientdet1(s.getPid());
  return list4;
}

getPatientdet1()从数据库中检索数据

public List<Iochart> getPatientdet1(String pid) {
  // TODO Auto-generated method stub
  System.out.println(pid);
  return template.query(
    "select pid,name,fileno,age,gender,date,wardno,doctord,doctsig,ratef,nursesig,time,type,amount,typecommence,amtgiv,urine,vomitus,remarks from iochart where pid='"+pid+"'",
    new RowMapper<Iochart>() {  
      public Iochart mapRow(ResultSet rs, int row) throws SQLException {   
        Iochart i = new Iochart();
        i.setPid(rs.getString(1));
        i.setName(rs.getString(2));
        i.setFileno(rs.getString(3));
        i.setAge(rs.getString(4));
        i.setGender(rs.getString(5));
        i.setAdmdate(rs.getString(6));
        i.setWardno(rs.getString(7));
        i.setDoctord(rs.getString(8));
        i.setDoctsig(rs.getString(9));
        i.setRatef(rs.getString(10));
        i.setNursesig(rs.getString(11));
        i.setTime(rs.getString(12));
        i.setOraltype(rs.getString(13));
        i.setOralamt(rs.getString(14));
        i.setOralcommence(rs.getString(15));
        i.setAmtgiv(rs.getString(16));
        i.setUrine(rs.getString(17));
        i.setVomitus(rs.getString(18));
        i.setRemarks(rs.getString(19));
        System.out.println(rs.getString(2));
        return i;
      }
        }
  );
}

1 个答案:

答案 0 :(得分:0)

更改您的控制器方法。注意用于获取pid的@RequestParam

@RequestMapping(value="/iochart1", method = RequestMethod.GET)
public @ResponseBody List<Iochart> iochart1(@RequestParam(name = "pid") String pid) {
    return dao.getPatientdet1(pid);
}

你的ajax网址如此

function doAjaxPost() {
    // get the form values
    var pid = $('#pid').val();
    $.ajax({
        type: "GET",
        url: "http://localhost:8085/HMS/iochart1", //Don't postfix .hmtl
        data: "pid=" + pid,
        ...
    });
}