簡體   English   中英

如何將列表從Spring Controller傳遞到jQuery中的Ajax

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

這是我的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;
      }
        }
  );
}

像這樣更改您的控制器方法。 注意用於獲取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,
        ...
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM