我试图用 json 进行 ajax 调用,下面是我的代码描述
$(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); */
}
});
}
});
服务器端代码是
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();
但是我的用户名和密码都为空值。请帮助我举例说明如何获取名称和密码服务器端。
如果我从请求中删除代码
contentType: "application/json; charset=utf-8",
dataType: "json",
我使用上面的服务器端代码获取名称和密码值。但我怎么能不删除
contentType: "application/json; charset=utf-8",
dataType: "json",