I have a problem with the AJAX success response what not work properly and I don't understand why.. I have a form generated with Javascript from a XML document using XML DOM and as action I put a servlet for doing an update to a database using form couple name-value. Once I click on the button, the servlet works perfectly and it updates correctly my DB but once I call AJAX function for displaying an alert message in the same page of the form, it doesn't work at all and it's not displayed. How can I solve it?
This is part of the form generated with Javascript (it's a part of a table)
...
txt = txt + "<td class=\"pull-right\">" + "<form name=\"form1\" method=\"get\" action=\"SetTrue\">" +
"<input type=\"hidden\" name=\"id\" value=\"" + nn[0].firstChild.nodeValue + "\"/>"
+ "<button type=\"submit\" class=\"btn btn-link glyphicon glyphicon-star\"></button>"
+ "</form>" + "</td>";
...
This is part of my servlet code that works fine
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
response.setContentType("text/html;");
PrintWriter out = response.getWriter();
Integer UserID = (Integer)session.getAttribute("iduser");
String errorMsg = null;
String successMsg = null;
try {
ConnectionManager conn = new ConnectionManager();
Connection connection = conn.getConnection();
PreparedStatement statement = null;
String string_id = request.getParameter("id");
int id = Integer.parseInt(string_id);
boolean set_true = true;
String update = "UPDATE Table SET Attribute=? WHERE ID=? AND UserID=?";
statement = connection.prepareStatement(update);
statement.setBoolean(1, set_true);
statement.setInt(2, id);
statement.setInt(3, UserID);
int num = statement.executeUpdate();
if (num > 0) {
successMsg = "OK";
out.println(successMsg);
RequestDispatcher reqDisp = getServletContext().getRequestDispatcher("/Welcome.jsp");
reqDisp.forward(request, response);
}
else {
errorMsg = "Error";
session.setAttribute("error", errorMsg);
RequestDispatcher reqDisp = getServletContext().getRequestDispatcher("/Welcome.jsp");
reqDisp.forward(request, response);
}
connection.close();
statement.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
This is my AJAX function in Welcome.jsp page
...
$("#form1").submit(function(){
$.ajax({
type : form.attr('method'),
url : form.attr('action'),
success : function(data) {
alert(data);
}
});
...
Could please anyone help me and solve my problem, explaining me why doesn't it work? Thanks in advance