ajax post 为空(数据库中的空字符串)

ajax post is empty(Empty String in Database)

我的 ajax 代码有问题。

$(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) 打开一个对话框,但它是空的,这意味着 data 是空字符串还是不是 available.Can 你能帮我解决这个问题吗?

如果有帮助,这是我的 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();
        }
    }
}

}

您似乎需要发送 "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);
        });
    });

使用浏览器实用程序检查请求。它将帮助您确定数据为空的原因。

     - 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
    });
   });
 })

我通过改变这个解决了这个问题

  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");

    }