atom2ueki
atom2ueki

Reputation: 837

Jquery Ajax call servlet not successful

Here is my servlet code

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

Here is my 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;
    });
});

Here is my form

<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>

After I click the submit button, from url I can see
admin.jsp?name=sasa&price=sasasa&description=sasasasa

But result shown nothing in console, seems my jquery never call my servlet, I don't know why PS: I'm using eclipse with tomcat server

Upvotes: 0

Views: 785

Answers (1)

AdityaKeyal
AdityaKeyal

Reputation: 1228

I see 2 issues to start with :

  1. $("#update").submit(fuction(){ should be $("#update").submit(function(){ - the n in function is missing
  2. $(#success).show(); should be $("#success").show(); the quotes are missing.

The rest of the code worked fine for me. The request was reaching the doPost method. I also captured the network request in IE and was able to see a request to server for write.
I should add one important point here : your #("update").serialize() is passing empty values to the server hence the SOP in your servlet is printing blank. Try to add a string constant to that like System.out.println("Values : "+name+price+description);

Upvotes: 3

Related Questions