我必须使用console.log(data)函数;在我的JavaScript中,但它很有效。有人知道怎么做吗?我刚刚将我的问题编辑成了一些更多的信息 最好的问候朱莉
<script type="text/javascript">
$(function() {
$(".datepicker" ).datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(date, instance){
},
});
$("input[name=endDate]").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(date, instance){
$.post( "../getHoursSQL", {start:$("#startDate").val(),end:$("#endDate").val()}, function( data ) {
console.log(JSON.stringify(data));
$( "#startresult" ).html( data );
},'json');
}
});
});
</script>
</head>
<body>
<form method="post" id="myForm" action="../getHoursSQL">
Start Date:
<input type="text" name="startDate" class="datepicker"/><br/>
End Date:
<input type="text" name="endDate" class="datepicker"/><br/>
<input type="submit"/>
</form>
<div id="startresult"></div>
<div id="endresult"></div>
</body>
控制台打印:
我的Servlet:
import java.io.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/getHoursSQL")
public class getHoursSQL extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
String connectionURL = "jdbc:mysql://localhost/NekiWork";
Connection connection=null;
try {
//Load database driver
Class.forName("com.mysql.jdbc.Driver");
//Connection to the database
connection = DriverManager.getConnection(connectionURL, "root", "");
//Getting the data from database
String sql = "SELECT *, (Day_hours + (Day_minutes / 60)) AS Allday_hours FROM Workdata "
+ " WHERE Date = ? AND Date = ? ";
PreparedStatement pst = connection.prepareStatement(sql);
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
java.util.Date util_startDate = format.parse( req.getParameter("startDate") );
java.sql.Date sql_startDate = new java.sql.Date( util_startDate.getTime() );
java.util.Date util_endDate = format.parse( req.getParameter("endDate") );
java.sql.Date sql_endDate = new java.sql.Date( util_endDate.getTime() );
pst.setDate( 1, sql_startDate );
pst.setDate(2, sql_endDate );
System.out.println(req.getParameter("startDate"));
System.out.println(req.getParameter("endDate"));
//Show the result from database
ResultSet rs = pst.executeQuery();
float Allday_hours_sum = 0;
while (rs.next()){
Allday_hours_sum += rs.getFloat("Allday_hours");
}
res.setContentType("text/html;charset=UTF-8");
res.getWriter().print(Allday_hours_sum);
pst.close();
}
catch(ClassNotFoundException e){
System.out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e){
System.out.println("SQLException caught: " + e.getMessage());
}
catch (Exception e){
e.printStackTrace();
}
finally {
try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
System.out.println(ignored);
}
}
}
}
POST SERVLET控制台图像
答案 0 :(得分:0)
暂时不要担心console.log
。查看浏览器开发人员控制台中网络选项卡的响应选项卡。
检查getHoursSQL
顺便说一下,不应该getHoursSQL.php
或类似的?
编辑:
在您的情况下 getHoursSQL.java