[英]Trying to post form data to servlet using JQuery
I am attempting to post data to a servlet where it will be inserted in to a mysql database. 我试图将数据发布到servlet,然后将其插入到mysql数据库中。
Here is the html form: 这是html形式:
<form id="commentForm" name="commentForm" action="http://server.co.uk/dbh" method="POST">
<input type="text" name="name" placeholder="Your name" required="required">
<textarea name="comment" placeholder="Enter your comment here" required="required"></textarea>
<input type="hidden" name="postID" id="postID" value="<%= postID %>">
<input type="submit" name="submit" id="commentSubmit" value="submit">
</form>
The Jquery: jQuery:
$("#commentSubmit").click(function(e){
var postData = $("#commentForm").serializeArray();
var formURL = $("#commentForm").attr("action");
$.ajax(
{
url : formURL,
type: "POST",
data : postData,
success:function(data, textStatus, jqXHR)
{
$("#commentFormWrap").html("<p>Success</p>");
},
error: function(jqXHR, textStatus, errorThrown)
{
$("#commentFormWrap").html("<p>error: "+errorThrown+"</p>");
}
});
e.preventDefault(); //STOP default action
$("#commentForm").hide();
});
A simplified dbh servlet: 简化的dbh servlet:
import javax.servlet.annotation.WebServlet;
...
@WebServlet(description = "Handles connection to MySql database", urlPatterns = { "/dbh" })
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
postArray = request.getParameterValues("postData");
commentName = postArray[0];
comment = postArray[1];
postID = Integer.parseInt(postArray[3]);
try{
submitComment();
}catch(Exception e){
e.printStackTrace();
}
}
public void submitComment() throws Exception{
try{
sql="INSERT INTO crm_comments (comment_name, comment_content, comment_date, post_id) VALUES (?, ?, NOW(), ?)";
prep = conn.prepareStatement(sql);
prep.setString(1, commentName);
prep.setString(2, comment);
prep.setInt(3, postID);
rs = prep.executeQuery();
}catch(Exception e){
e.printStackTrace();
}
}
Currently the ajax
call is returning the error block error:
. 当前,
ajax
调用返回错误块error:
。 But nothing as the errorThrown
variable. 但是没有什么作为
errorThrown
变量。 From what I can see the servlet is written correctly. 从我可以看到的servlet是正确编写的。 Is there something wrong between the
html
and the Jquery
ajax
call, that it isn't posting the data to the servlet? html
和Jquery
ajax
调用之间是否有问题,即没有将数据发布到servlet?
postArray = request.getParameterValues("postData");
I think you can replace this with actual field names 我认为您可以将其替换为实际的字段名称
commentName = request.getParameter("name");
comment = request.getParameter("comment");
this will solve your problem 这将解决您的问题
According to the jQuery documentation , the serializeArray
method returns a JavaScript array of objects. 根据jQuery文档 ,
serializeArray
方法返回一个JavaScript对象数组。 This method will generate a JSON object from your form
, that will look like: [{"name":"name",value:"<your name input value>"},{"name":"comment",value:"<your comment>"},{"name":"postID",value:"<postID value>"} ...]
. 此方法将从您的
form
生成一个JSON对象,该对象将类似于: [{"name":"name",value:"<your name input value>"},{"name":"comment",value:"<your comment>"},{"name":"postID",value:"<postID value>"} ...]
。 Only the content of the postData
variable will be sent: you won't have any reference at all to that postData
keyword in your Servlet. 仅发送
postData
变量的内容:您在Servlet中将完全没有对该postData
关键字的引用。
So in your Servlet, the request.getParameterValues("postData");
因此,在您的Servlet中,
request.getParameterValues("postData");
call seems to be invalid. 通话似乎无效。 Try this instead:
尝试以下方法:
commentName = request.getParameterValues("name");
comment = request.getParameterValues("comment");
postID = Integer.parseInt(request.getParameterValues("postID"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.