1

我已经将 ajax 与 jquery 一起使用,并且我的 ajax url 正在调用另一个 jsp 页面,该页面将表单数据提交到数据库问题是每当我运行插入查询时,它的回复“无法将空值插入数据库”有点异常,但是每当我重新加载同一页面并单击提交按钮时,它都会被提交到数据库。我认为问题出在回发方法之类的东西上,我是 ajax 新手,所以请需要您的帮助...

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#save').click(function ()
        {
            $.ajax({
                type: "post",
                url: "test.jsp", //this is my servlet
                data: "input=" +$('#id').val()+"&output="+$('#op').val(),
                success: function(msg){      
                        $('#output').append(msg);
                }
            });
        });

    });
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
    input:<input id="id" type="text" name="" value="" /><br></br>
    output:<input id="op" type="text" name="" value="" /><br></br>
    <input type="button" value="save" name="save" id="save"/>
   <div id="output"></div>
</body>

JSP:

<%@page import ="com.connectionUtil.ConnectionUtil"%> 
<!DOCTYPE html> 
<script src="js/jquery.min.js" type="text/javascript"></script> 
<% String id = request.getParameter("input");
    try {
        Connection con = ConnectionUtil.getConnection();
        String sql = "insert into test(ID,...) values ('" + id + "',...)";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.executeUpdate();
    } catch (SQLException sqe) {
        out.println(sqe);
    }%> 
<body> <h4><%=id%> is stored </h4> </body> </html>
4

2 回答 2

3

你的ajax代码是错误的。它应该是这样的:

$.ajax({
  type: "post",
  url: "test.jsp", //this is my servlet
  data: {input : $('#id').val(), output: $('#op').val()},
  success: function(msg){      
      $('#output').append(msg);
  }
});

然后使用request.getParameter("input")获取参数值

更新:请注意,我在数据行末尾错过了逗号(,):....

更新 2:这是您的代码的工作演示,没有与 db 的连接...

index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#save').click(function ()
        {
            $.ajax({
                type: "post",
                url: "test.jsp", //this is my servlet
                data: {
                    input: $('#id').val(), 
                    output: $('#op').val()
                },
                success: function(msg){      
                        $('#output').append(msg);
                }
            });
        });

    });
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
    input:<input id="id" type="text" name="" value="" /><br></br>
    output:<input id="op" type="text" name="" value="" /><br></br>
    <input type="button" value="save" name="save" id="save"/>
   <div id="output"></div>
</body>

测试.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% String id = request.getParameter("input");
String output = request.getParameter("output");
%> 
<%=id%> is stored <br/>output is:<%=output%>

第一个屏幕显示:

在此处输入图像描述

插入数据并按下save按钮后,它会显示:

在此处输入图像描述

于 2013-07-22T09:59:26.500 回答
1

抱歉,我不会将此作为答案发布,但我必须这样做,因为我的帐户没有权限将其作为评论发布,但我确信您混淆了要点击的内容。#call指的是不是element div实际的按钮,但它也可以工作。您能否发布您test.jsp的代码以便我们查看代码,因为我怀疑这会导致问题。

我认为您已经编辑了您的问题,所以我认为我的回答很浪费。

于 2013-07-22T08:48:30.993 回答