Jquery Ajax调用servlet不成功

时间:2014-04-25 04:07:06

标签: java jquery ajax jsp servlets

这是我的servlet代码

@WebServlet("/write")
public class write extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public write() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

       /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String name = request.getParameter("name");
            String price = request.getParameter("price");
            String description = request.getParameter("description");
            System.out.println(name+price+description);
        }

这是我的ajax jquery

$( document ).ready(function() {
    var mydata = $("#update").serialize();
    $("#update").submit(fuction(){
        $.ajax({
            url: 'write',
            data: mydata,
            type: 'POST',
            success: function(data){
                $(#success).show();
            }
        });
        return false;
    });
});

这是我的表格

<form id="update">
    <fieldset>
        <label for="name">Name</label>
        <input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all">
        <label for="price">Price</label>
        <input type="text" name="price" id="price" value="" class="text ui-widget-content ui-corner-all">
        <label for="description">Description</label>
        <input type="text" name="description" id="description" value="" class="text ui-widget-content ui-corner-all" >
    </fieldset>
    <input type="submit" class="ui-button" value="Save it!">
</form>

点击提交按钮后,从网址中我可以看到admin.jsp?name=sasa&price=sasasa&description=sasasasa

但是结果在控制台中没有显示,似乎我的jquery从不调用我的servlet,我不知道为什么 PS:我正在使用带有tomcat服务器的eclipse

1 个答案:

答案 0 :(得分:3)

我首先看到2个问题:

  1. $("#update").submit(fuction(){应为$("#update").submit(function(){ - 功能中的n缺失
  2. $(#success).show();应该$("#success").show();引号丢失。
  3. 其余的代码对我来说很好。请求到达了doPost方法。我还在IE中捕获了网络请求,并且能够看到服务器写入的请求 我应该在这里添加一个重点:#(“update”)。serialize()将空值传递给服务器,因此servlet中的SOP打印为空白。尝试将字符串常量添加到System.out.println("Values : "+name+price+description);

    之类的常量