0
我已經試圖與JSON Ajax調用下面是我的代碼詳細描述獲取Java Web應用JSON數據服務器端
$(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",
問題是什麼? – ahmedalkaff
如果你只是刪除'contentType'屬性呢?由於您沒有向服務器發送JSON字符串,因此不需要指定。 'dataType'屬性指定了jQuery應該返回的內容,並且因爲您似乎正在發回JSON,所以應該沒問題。 –
謝謝刪除內容類型的工作fine.can你建議我們如何檢索服務器端的json字符串。 – user1608528