Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jsp ajax call using jquery

I have this code snippet where I am passing data to another jsp file.

Javascript

$(document).ready(function() {
    $("#click").click(function() {
        name = $("#name").val();
        age = $("#age").val();
        $.ajax({
            type : "POST",
            url : "pageTwo.jsp",
            data : "name=" + name + "&age=" + age,
            success : function(data) {
                $("#response").html(data);
            }
        });
    });     
});     

HTML

<body>
    Name:<input type="text" id="name" name="name">
    <br /><br /> 
    Age :<input type="text" id="age" name="age">
    <br /><br />
    <button id="click">Click Me</button>
    <div id="response"></div>
</body>

and in pageTwo.jsp, my code is

 <%
   String name = request.getParameter("name");
   String age = request.getParameter("age");
   out.println(name + age);
 %>

but this is not working.Is any mistake in my Jquery ?.Can any one please help me?.

like image 287
edaklij Avatar asked Dec 28 '12 03:12

edaklij


2 Answers

$("#click").click(function(e) {
    // e.preventDefault();
    ...
    return false;
});

and of course install firebug or use chrome default developer tools (f12). open console and run the code.

like image 58
HamidRaza Avatar answered Oct 11 '22 21:10

HamidRaza


$(document).ready(function () {
    $("#click").click(function () {
        name = $("#name").val();
        age = $("#age").val();
        $.ajax({
            type: "POST",
            url: "pageTwo.jsp",
            data: "{'name':'" + name + "','age':'" + age + "'}",
            contentType: "application/json",
            async: false,
            success: function (data) {
                $("#response").html(data.d);
            }
        });
    });
});
like image 25
Prashant16 Avatar answered Oct 11 '22 23:10

Prashant16