我已经将 ajax 与 jquery 一起使用,并且我的 ajax url 正在调用另一个 jsp 页面,该页面将表单数据提交到数据库问题是每当我运行插入查询时,它的回复“无法将空值插入数据库”有点异常,但是每当我重新加载同一页面并单击提交按钮时,它都会被提交到数据库。我认为问题出在回发方法之类的东西上,我是 ajax 新手,所以请需要您的帮助...
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#save').click(function ()
{
$.ajax({
type: "post",
url: "test.jsp", //this is my servlet
data: "input=" +$('#id').val()+"&output="+$('#op').val(),
success: function(msg){
$('#output').append(msg);
}
});
});
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
input:<input id="id" type="text" name="" value="" /><br></br>
output:<input id="op" type="text" name="" value="" /><br></br>
<input type="button" value="save" name="save" id="save"/>
<div id="output"></div>
</body>
JSP:
<%@page import ="com.connectionUtil.ConnectionUtil"%>
<!DOCTYPE html>
<script src="js/jquery.min.js" type="text/javascript"></script>
<% String id = request.getParameter("input");
try {
Connection con = ConnectionUtil.getConnection();
String sql = "insert into test(ID,...) values ('" + id + "',...)";
PreparedStatement ps = con.prepareStatement(sql);
ps.executeUpdate();
} catch (SQLException sqe) {
out.println(sqe);
}%>
<body> <h4><%=id%> is stored </h4> </body> </html>