Ajax传递List对象到前台展示问题遇到的坑

后台Json转换

后台传递的List对象,如果对象是实体类,实体类和另一个表关联,就可能会出现以下错误
  org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: forum.po.Topic.replyList, could not initialize proxy - no Session

所以最好新建一个VO 视图类,将需要展示的字段单独拿出来(可以写到VO类的构造方法里去),再转换成json串。
如果使用fastjson的话,转换语句为:JSON.toJSONString(topicList)

 

前端jquery循环遍历

前端获取的json数据格式为 [{"":"","":""},{"":"","":""}]

在循环遍历以前,需要先JSON.parse() 解析json字符串,再用$.each遍历。

var topicList =JSON.parse(result.body);
$.each(topicList, function(i,topic) {
    console.log(topic);
    console.log(topic.title);
});

 

写到页面中

最后还要将数据写到页面上,注意jquery的语法

var target_ul = $("#topic_value_ul");
var res = [];
var topicList =JSON.parse(result.body);
$.each(topicList, function(i,topic) {
    res.push('<li style="text-align: left;">');
    res.push('<strong><span style="color: #2d64b3;font-size:16px;">' + topic.title + '</span></strong>');
    res.push('</li>');
});
target_ul.empty().html(res.join(""));

 

原创文章,欢迎转载,转载请注明出处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值