经过Spring MVC MappingJacksonJsonView转化后返回的JSON数据:
{"users": [ {"userid": null,"username": "user_01","pass": "123" }, {"userid": null,"username": "user_02","pass": "456" } ] }
Spring MVC控制器处理来自“test/getJSON”的请求:
@RequestMapping("/getJSON") public ModelAndView getJSON() { System.out.println("getJSON"); ModelAndView mav = new ModelAndView(); List<User> users = this.getUsers(); mav.addObject("users", users); mav.setViewName("testJSON"); return mav; }
在页面读取返回数据:
<script type="text/javascript"> $(function(){ $("#testJson").click(function(){ $.ajax({ type:"GET", url:"http://localhost:8080/SpringTest/test/getJSON?content=json", dataType:"json", contentType:'application/json;charset=UTF-8', success:function(data){ $("#result").empty(); var strResult = ""; var users = data.users; for(index in users){ strResult += "<div class='comment'>用户名:" + users[index].username + "<br />密码:" + users[index].pass + "<hr /></div>"; } $("#result").html(strResult); }, error:function(){ alert("error."); } }); }); })</script>
在配置$.ajax()的url时,我写的url明显是冗长的。但是目前我还没有找到一个正确的、合理的处理方式。
作者: longying2008
声明: 本文系ITeye网站发布的原创文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
已有 0人发表回复,猛击->> 这里<<-参与讨论
ITeye推荐