简体   繁体   English

如何将列表从Spring Controller传递到jQuery中的Ajax

[英]How to pass a list from spring controller to ajax in jquery

This is my Jquery! 这是我的Jquery! Here i am not getting success response, instead a error response is thrown when a list is returned from controller to below jquery. 在这里,我没有得到成功响应,而是当列表从控制器返回到下面的jquery时引发了错误响应。 Please let me know where i am going wrong. 请让我知道我要去哪里错了。

//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);
    }
  });
}

Below is the controller which returns the list. 下面是返回列表的控制器。

@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() which retrieves the data from database 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;
      }
        }
  );
}

Change your controller method like so. 像这样更改您的控制器方法。 Mind the @RequestParam that is used to get the pid 注意用于获取pid的@RequestParam

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

And your ajax url like so 和你的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,
        ...
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM