doGet设置项readState新值并将其作为Json对象发送回ajax调用:
PrintWriter out = null;
try {
out = resp.getWriter();
Integer read = Integer.parseInt(req.getParameter("feedReadVal"));
Gson data = new Gson();
JsonObject jsonObj = new JsonObject();
Integer readState = null;
Item item = new Item();
readState = 1;
item.setReadState(readState.byteValue());
JsonElement element = data.toJsonTree(item);
jsonObj.add("read", element);
out.print(jsonObj);
} catch (Exception e) {
e.printStackTrace();
}
通话:
System.out.println("JSON OBJECT: " + jsonObj.toString());
显示类似JSON OBJECT: {"read":{"readState":1}}
的内容,但当我尝试获取成功函数错误时抛出。我的jQuery:
var data = "feedReadVal=" + itemRead + "&feedItemId=" + feedItemId
+ "&action=" + 'read';
$.ajax({
type : "Get",
url : "FeedItemController",
data : data,
dataType : "json",
success : function(data, textStatus, jqXHR) {
alert(data.read.readState);
},
error : function(jqXHR, textStatus, errorThrown) {
alert("Something really bad happened ");
}
}).done(function() {
});
我一直收到错误提醒。
答案 0 :(得分:0)
您需要将其写入流
,而不是打印将out.print(jsonObj);
更改为
out.write(jsonObj.toString());
out.flush();