0

i have trying to make ajax call with json and below is my code discription

$(document).ajaxStart(function () {

    $("#innerpanel").html("<img class='test' src='Image/ajax-loader_clock.gif' alt='loading...' />");});

    /* $("#loader").ajaxStop(function () {
        $(this).hide();
    }); */

    $('#btn').click(function(){
        testService();
     });

    function  testService(){  
        name=$("#name").val();
        password=$("#password").val();
        var testData={name : name,password : password}; 

        $.ajax({
            type: "post",
            url: "login.do",
             data:testData,
             contentType: "application/json; charset=utf-8",
            dataType: "json", 
            success: function(msg) {
                alert("madhav");
                $("#innerpanel").html("<p1>Welcome</p1> </br>"+"<p1>"+msg.name+"</p1></br><p1> and Your password is "+msg.password+"</p1>");
                /* document.writeln("Book id: " +  msg.name); */
            }
        });
    }
});

and sever side code is

String userName=request.getParameter("name");
String password=request.getParameter("password");
PrintWriter writer = response.getWriter();
JSONObject obj=new JSONObject();

obj.put("name", userName);
obj.put("password",password);
writer.print(obj);
writer.flush();

But i get null value for both username and password .please help me with example how can I get name and password server side.

if i remove code from request

    contentType: "application/json; charset=utf-8",
    dataType: "json", 

i get name and password value with above server side code. but how can i do it without removing

    contentType: "application/json; charset=utf-8",
    dataType: "json",
2
  • What if you just remove the contentType property? There's no need for you to specify that since you're not sending a JSON string to the server. The dataType property specifies what jQuery should expect back, and since you appear to be sending back JSON that should be fine. Commented Aug 22, 2013 at 11:56
  • thanks after removing content type its working fine.can you suggest how could we retrieve json string at server side. Commented Aug 22, 2013 at 12:26

1 Answer 1

1
var testData = {"name" : name, "password" : password}; 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.