我是ajax的新手,我想从我的jsp页面调用ajax到servlet,然后在同一页面上的表中显示数据。当我创建一个ajax调用时,url路径转到servlet但状态显示错误在回调函数中。我想我无法在ajax调用中设置数据类型,请告诉我流程以及我在ajax调用中错误的位置,所有帮助都要提前感谢。
我的代码是:
var postData = $('#ajaxform').serializeArray();
$
.ajax({
type: 'POST',
url : "attendancePercentage",
data : postData,
dataType:'html',
success : function(data, status) {
alert(status);
alert(data);
}
});
servlet:在控制台中,我在s.o.p语句中获取数据,但是ajax调用不成功。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String stDate=request.getParameter("startDateValue");
String enDate=request.getParameter("endDateValue");
System.out.println(""+stDate+""+enDate+"");
List stList = new ArrayList();
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));
stList.add(ge);
}
System.out.println("list>>"+stList);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}