【问题标题】:ajax post is empty(Empty String in Database)ajax 帖子为空(数据库中的空字符串)
【发布时间】:2015-01-02 10:52:11
【问题描述】:

我的 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 是一个空字符串或不可用。你能帮我解决这个问题吗?

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

}

【问题讨论】:

  • 你检查你的servlet中name变量的值吗?
  • 您的data: 调用中没有data: 参数。所以request.getParameter("getdata") 什么也找不到。
  • getData 是我的 的名称我如何在数据中引用它:?也许数据:{“名称”:“getdata”}?

标签: javascript jquery ajax servlets


【解决方案1】:

看来您需要使用 ajax 发送“getdata”和“submitdata”参数。

在您的服务器端代码中,如果该参数不存在,您只需发回一个空字符串:

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

【讨论】:

  • 也是submitdata 参数。
  • @Rupert 谢谢你的回答,但它仍然是空的:(我也没有错误或其他东西,所以我真的不知道该怎么办......
  • 你能发布你的 HTML 吗? submitdata 是提交按钮吗? serialize() 不会包含它,它只会从表单中获取数据字段。
  • 哦,对不起,我不知道如何在 cmets O..o 中格式化代码
【解决方案2】:

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

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

【讨论】:

  • OK tab post显示: Parameter getdata testdata Source getdata=testdata , and Respone tab is empty
  • 这可能对你有帮助javatutorials.co.in/…
  • 我在 XML 选项卡中遇到错误:XML-Verarbeitungsfehler: Kein Element gefunden Adresse: moz-nullprincipal:{f2c4ef5d-acd5-4ec8-8470-1b9973c1a0c0} Zeile Nr. 2、Spalte 1:
  • 响应现在是正确的,但在我提交后它会将我引导到带有我的输入的空白​​页面,但我想在我的表上显示此输入
【解决方案3】:

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

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

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-12
    • 2018-09-05
    • 2017-12-20
    • 2013-12-23
    • 2013-03-31
    • 2021-12-09
    • 2017-09-21
    • 1970-01-01
    相关资源
    最近更新 更多