這裏在我的代碼中,我將json格式的用戶名和密碼提交給restful service get方法。作爲回報,此方法返回json對象,我無法在jquery的兩個文本框中顯示該對象。在jquery中打印json對象的值
這是我的jsp頁面:
<script>
$(document).ready(function() {
$("#btnSubmit").click(function() {
var uname = $("#uname").val();
var pwd = $("#pwd").val();
authenticate(uname, pwd);
});
});
//authenticate function to make ajax call
function authenticate(uname, pwd) {
$.ajax
({
type: 'GET',
//the url where you want to sent the userName and password to
url: 'REST/WebService/GetLogin',
dataType: 'json',
async: false,
//json object to sent to the authentication url
data:{'uname': uname, 'pwd': pwd },
error:function()
{
alert("unsuccessfull");
},
success:function(server_response)
{
$('#message').text(server_response.message);
$('#lastname').text(server_response.lastname);
alert("successfull");
}
});
}
</script>
<input type="text" id="message"></input><br/>
<input type="text" id="lastname"></input><br/>
UserName:<input type="text" name="uname" id="uname"/><br/>
Password:<input type="text" name="pwd" id="pwd"/><br/>
<input type="submit" name="btnSubmit" id="btnSubmit" value="LogIn"/>
我的寧靜get方法:
@Path("/WebService")
public class LoginService{
@GET
@Path("/GetLogin")
@Consumes("application/json")
@Produces("application/json")
public String loginFeed(@QueryParam("uname") String uname,@QueryParam("pwd") String pwd)
{
String feeds = null;
try
{
ArrayList<FeedObjects> feedData = null;
ProjectManager projectManager= new ProjectManager();
feedData=projectManager.GetLogin(uname, pwd);
feeds=FeedTransformer.UserFeed(feedData);
System.out.println("inside");
}
catch (Exception e)
{
System.out.println("error");
}
return feeds;
}
這飼料是JSON對象的形式。
你會得到什麼,而不是例外的結果?任何錯誤?我建議使用Firefox插件「Firebug」,它允許你分析網絡通信。這樣你就可以在Firebug中看到你的JSON結果。你的服務器部分使用什麼標題?如果它不是JSON,你可能要告訴jQuery結果將以JSON格式,或者你可能想使用'var server_message_json = $ .parseJSON(server_message); **編輯:**只是注意到你實際上**確實**提交數據類型。無論如何,螢火蟲可能會讓你免於許多充滿頭痛的睡眠之夜。 –
我的迴應是在JSON中從安寧的服務實現,即@Produces(「application/json」) – shanky