日期:2014-05-16  浏览次数:20379 次

jquery异步请求返回json数据类型
JSP页面:
 $.get("/kcm/common/tree2getChild/",
                                {"id":id},
                                function(data){
                                    var html = makeHtml(data);
                                    //回调函数
                                },
                         "json");

 function makeHtml(data) {
            var str="";
            for(var i=0; i<data.length; i++) {
                var html="";
                var par=data[i].pId;
                var hasChild = data[i].hasChild;
                //alert(hasChild);
                
                html= "<tr id = '" + data[i].id +"'";
                //not root
                if(par != 0) {
                    html = html + " pId='" + par +"'";
                }
                if (hasChild != 0) {
                    html = html + " hasChild='true' ";
                }
                html = html + "><td></td><td>" + data[i].name +"</td></tr>";
                
                str += html;
            }
            //alert(str);
            return str;
         }


服务器端tree2getChild访问的方法:
    @Execute(validator = false)
    public String index() {

        String id = request.getParameter("id");

        try {
            if (id == null) {
                String strJson;
                strJson = JSONUtil.fromList(getRootList());

                wirteToResponse(response, strJson);
            } else {
                String strJson = JSONUtil.fromList(getChildList(id));
                wirteToResponse(response, strJson);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }

        return null;
    }

    public void wirteToResponse(HttpServletResponse response, String jsonObj) {
        PrintWriter out = null;

        response.setContentType("text/html;charset=utf-8");

        try {
            out = response.getWriter();
            out.print(jsonObj);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }