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",
contentTypeproperty? There's no need for you to specify that since you're not sending a JSON string to the server. ThedataTypeproperty specifies what jQuery should expect back, and since you appear to be sending back JSON that should be fine.