【发布时间】:2016-05-16 02:31:38
【问题描述】:
我正在构建类似于储物柜管理系统的东西,并且储物柜有更新历史记录。我想使用 Ajax 返回此历史记录的列表,最多为列表中的 X 个条目。每当我返回List<LockerHistoryEntity> 或Iterable<LockerHistoryEntity> 时,都会返回对象的数量,但所有的对象都是空的。
Ajax 调用(在这种情况下使用 getHistory(100) 调用):
function getHistory(limit) {
var data = {};
data["limit"] = limit;
$.ajax({
type: "POST",
contentType: "application/json",
url: "/gethistory",
data: JSON.stringify(data),
dataType: 'json',
timeout: 100000,
success : function(data) {
fillTable(data);
console.log(data);
console.log(data.result);
},
error : function(e) {
//error
}
});
}
@RestController
@JsonView(Views.Public.class)
@RequestMapping(value = "/gethistory", method = RequestMethod.POST, produces="application/json")
@ResponseBody
public Iterable<LockerHistoryEntity> getHistory(@RequestBody HistoryLimit limit) {
Iterable<LockerHistoryEntity> lockerHistory;
if (limit.getLimit() >= 0) {
lockerHistory = history.findAllLimit(limit.getLimit());
} else {
lockerHistory = history.findAll();
}
return lockerHistory;
}
当使用for循环打印出lockerHistory时,数据全部正确显示。
遗憾的是,我找不到关于这个确切问题的任何主题,因此这篇文章。
【问题讨论】:
-
您是否尝试将对象转换为 JSON?我在这里找到了一些有用的链接baeldung.com/spring-httpmessageconverter-rest 希望对您有所帮助
-
谢谢,确实解决了。忘记解析为 JSON。
标签: javascript jquery json ajax spring