我无法使用 Json 对象从 java servlet 的 ajax 请求中取回数据。下面是我们在谷歌应用引擎中使用通道 api 的代码。我们需要实现聊天应用程序。
displayFriendList = function() {
var txt = document.createElement("div");
txt.innerHTML = "<p> Logged in as <b>" + userid
+ "</b><p><hr />";
document.getElementById("friendlistdiv").appendChild(
txt);
var dataString ='userId='+userid;
$.ajax({
type : "POST",
url : "/OnlineUserServlet",
data : dataString,
success : function(html) {
alert(html.frndsList[0].friend);
}
});
};
Java Servlet 代码:
while(friendList.hasNext()){
friend = friendList.next() ;
if(!friend.equals(user)){
Map<String, String> state = new HashMap<String, String>();
state.put("friend", friend);
state.put("type","updateFriendList");
state.put("message",user);
state1.add(state);
message = new JSONObject(state);
channelService.sendMessage(
new ChannelMessage(friend,message.toString()));
}
i++;
}
Map<String, String> statejason = new HashMap<String, String>();
statejason.put("friendsList", state1.toString());
//System.out.print("hello"+message.toString());
response.setContentType("text/plain");
response.getWriter().print(statejason.toString());
}