簡體   English   中英

jQuery Ajax調用servlet不成功

[英]Jquery Ajax call servlet not successful

這是我的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

我看到2個問題開始於:

  1. $("#update").submit(fuction(){應該是$("#update").submit(function(){ -函數中的n缺失
  2. $(#success).show(); 應該是$("#success").show(); 引號丟失。

其余的代碼對我來說很好。 該請求已到達doPost方法。 我還捕獲了IE中的網絡請求,並且能夠看到對服務器的寫入請求。
我應該在此處添加一個要點:您的#(“ update”)。serialize()將空值傳遞給服務器,因此servlet中的SOP打印空白。 嘗試添加一個字符串常量,例如System.out.println("Values : "+name+price+description);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM