我的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)
打开一个Dialog,但它是Empty,这意味着data
是一个空字符串或不可用。你可以帮我解决这个问题吗?
如果有帮助,这是我的Servlet代码:
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();
}
}
}
}
答案 0 :(得分:0)
看起来你需要发送" getdata"和" submitdata"参数与ajax。
在服务器端代码中,如果该参数不存在,则只返回一个空字符串:
if(request.getParameter("submitdata") != null) {
if(request.getParameter("getdata") != null) {
name += request.getParameter("getdata");
}
}
你的ajax应该是这样的:
$("#ajaxtest").submit(function(ev) {
var data = $(this).serialize();
data.submitdata = true;
ev.preventDefault();
$.ajax({
type:"POST",
url:"InsertServlet",
dataType: "text",
data: data
})
.done(function(data){
alert(data);
$(activeTD).text(data);
});
});
答案 1 :(得分:0)
使用浏览器实用程序检查请求。它将帮助您确定数据为空的原因。
- Launch the chrome browser
- Right click and select inspect element menu
- click on Network tab
- Load your URL
- Perform the Ajax request
- You can see the request here (new request will be last in the list).
- Click on it
- Right side window shows you request and response data
编辑:
$("#ajaxtest").submit(function(ev) {
var data = $(this).serialize();
data.submitdata = true;
ev.preventDefault();
$.ajax({
type:"POST",
url:"InsertServlet",
dataType: "text",
data: data,
success:function(data){ // added the success call back
console.log(data); // check this in browser console
});
});
})
答案 2 :(得分:0)
我通过更改此
解决了这个问题 if(request.getParameter("submitdata") != null) {
if(request.getParameter("getdata") != null) {
name += request.getParameter("getdata");
}
}
到这个
String name="";
if(request.getParameter("getdata") != null) {
name += request.getParameter("getdata");
}