서블릿에 데이터를 게시하려고 시도하고 있습니다. 서블릿에 데이터가 삽입 될 데이터를 mysql 데이터베이스에 게시하려고합니다.JQuery를 사용하여 양식 데이터를 서블릿에 게시하려고 시도했습니다.
<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>
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();
});
단순화 DBH 서블릿 :
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();
}
}
현재 ajax
호출이 에러 블록 error:
복귀 여기
errorThrown
변수는 없습니다. 서블릿이 올바르게 작성된 것을 볼 수 있습니다.
html
과
Jquery
ajax
호출 사이에 뭔가 잘못 되었습니까? 서블릿에 데이터를 게시하지 않습니다.
는 브라우저 콘솔에서 오류를하지 마세요 문제를 해결할 수는 실제 필드 이름
와 이것을 대체 할 수 있다고 생각 또는 서버 측에서? – cy3er
eclipse를 사용하고 ajax 호출을 제출할 때 콘솔에 오류가 없습니다. – crm
다른 서블릿이 애플리케이션에서 제대로 작동하고 있습니까? – cy3er