`
lxf_2011
  • 浏览: 60709 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

服务器端返回json数据

    博客分类:
  • Java
阅读更多

   import flexjson.JSONSerializer;



@Inject
    RegionService regionService;

    @RequestMapping(value = "/region/data/{leval}/{id}", method = RequestMethod.POST)
    public void getRegion(HttpServletResponse response,
            HttpServletRequest request, @PathVariable String id,@PathVariable int leval) {

        PrintWriter writer = null;
        try {
            log.debug("into getRegion method...");
            log.debug("id is : " + id);
            response.setContentType("text/html;charset=utf-8");
            writer = response.getWriter();
            String outStr = "";
            List<Region> list = regionService.queryById(id, leval);

// 把list 转化成json数据,写到流中。前台使用
            outStr = new JSONSerializer().exclude("*.class")
                    .exclude("*.description").exclude("*.other")
                    .exclude("*.sId").exclude("*.leval").exclude("*.pId").exclude("*.cId")
                    .serialize(list);
//exclude() 不包括哪些字段,
            log.debug("outStr : " + outStr);
            writer.write(outStr);

        } catch (Exception e) {
            log.error("{}", e.getMessage());
        } finally {
            writer.close();
        }

        writer.flush();
        writer.close();
    }



前台:

//异步获取地区区域
    $("#shen").change(function(){
        var id = $("#shen").val();
        $.ajax({
               type: "POST",
               url: "/app/region/data/3/"+id,
               success: function(list_str){

                   // 字符串转化成json数据
                   eval("var list="+list_str);
                   if(list_str!=null){
                      for(var i=0 ;i<list.length;i++){
                          if(i==0){
                              $("#shi").empty();
                          }
                          $("#shi").append("<option value="+list[i].id+">"+list[i].name+"</option>");
                      }
                   }
               }
            });
    });

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics