我在ajax调用中遇到小麻烦,即无法在ajax中设置数据类型,对于字符串和文本数据类型,我得到ajax调用成功,但是当我从servlrt返回列表ajax调用失败时,请告诉我如何解决它,提前感谢任何帮助。
$
.ajax({
type: 'GET',
url : "attendancePercentage",
dataType: "json",
data : {"stdate": stdate, "endate": endate},
success : function(data, status) {
alert(status);
alert(data);
}
});
的Servlet
List<StudentMark> stList = new ArrayList<StudentMark>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test_schema", "root", "root");
String sql = "select * from attendance where attendance.date>=? and attendance.date<=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, stDate);
pstmt.setString(2, enDate);
rs = pstmt.executeQuery();
while (rs.next()) {
StudentMark ge = new StudentMark();
ge.setStudentName(rs.getString(3));
ge.setDate(rs.getString(2));
ge.setStatus(rs.getString(4));
//System.out.println(("&&&&&..." + ge));
stList.add(ge);
//System.out.println("ge :" + ge.getStudentName()+","+ge.getDate()+","+ge.getStatus());
}
System.out.println("list>>"+stList);
System.out.println("list>>"+json);//[{"","",""}]
} catch (Exception e) {
System.err.println(e.getMessage());
}
我正在上面的s.o.p语句中获取输出,如何设置此json对象的数据类型。
答案 0 :(得分:0)
您可以在StudentMarsk Class中覆盖toString @覆盖 public String toString(){ 返回new Gson()。toJson(this); }