vendredi 2 janvier 2015

ajax post is empty(Empty String in Database)


I have a Problem with my ajax-Code .



$(document).ready(function() {
var activeTD;
$(".td_test").click(function() {
$("#bModal").modal('show');
activeTD = this;
return false;

});
$("#ajaxtest").submit(function(ev) {
ev.preventDefault();
$.ajax({
type:"POST",
url:"InsertServlet",
dataType: "text",

})
.done(function(data){
alert(data);
$(activeTD).text(data);
});
});
});


alert(data) opens a Dialog but its Empty, which means that data is either an empty String or not available.Can you help me out with this?


If it helps, this is my Servlet Code :



public class InsertServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
processRequest(request,response);
}

protected void doGeT(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
processRequest(request,response);
}

protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
response.setContentType("text/plain");
String name="";
if(request.getParameter("submitdata") != null) {


if(request.getParameter("getdata") != null) {
name += request.getParameter("getdata");

}
}
PrintWriter out = response.getWriter();
out.println(name);
out.close();

String URL ="jdbc:mysql://localhost:3306/test";
String username = "root";
String pw= "root";

Connection con = null;
PreparedStatement stmt = null;
ResultSet rs =null;

try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(URL,username,pw);


stmt = con.prepareStatement(
"INSERT INTO ver(TName) VALUES(?);");
stmt.setString(1,name);
stmt.executeUpdate();
stmt.clearParameters();
stmt.close();
}catch(Exception e) {

}finally {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


}





Aucun commentaire:

Enregistrer un commentaire