当前位置: 代码迷 >> Java Web开发 >> JSON,AJAX
  详细解决方案

JSON,AJAX

热度:3776   发布时间:2013-02-25 21:22:59.0
求助JSON,AJAX
Java code
//这个是根据ID查询出来的一个Namepublic ActionForward getNameBySID(ActionMapping mapping, ActionForm form,            HttpServletRequest request, HttpServletResponse response) {        HttpSession session = request.getSession();                String deptName = "";        String deptLevel = request.getParameter("deptLevel");        //String deptLevel ="0001";        try {            deptName = deptService.GetDeptNameByDeptLevel(deptLevel);        } catch (BusinessServiceException e) {            e.printStackTrace();        }            System.out.println(deptName+"&&&&&&&"+deptLevel);        JSONObject json = new JSONObject();        json.put("json", deptName);                return null;    }


JScript code
function onNodeSelect(sID){    alert(sID);        if(sID.indexOf("000")==0){        $.ajax({        type:"post",        url:"${pageContext.request.contextPath}/measureWorkflow.do",        cache:false,        dataType:"json",        data:"method=getNameBySID&deptLevel="+sID,        success:function(data){        alert(${deptName});        window.frames["process"].document.write        ('<input type="text" value="aa"><br>');        }        });}    }
我现在在JS的Alert();里面得到DeptName,后台的方法已经得到,可是怎么样存这个数据,前台才能得到呀,Session,request不行的,我试过了?求助,只要回答正确,多少分我都给,先谢谢各位!

------解决方案--------------------------------------------------------
要是json不能这样写
Java code
JSONObject json = new JSONObject(); json.put("json", deptName);return json;
------解决方案--------------------------------------------------------
呵呵,你的问题缺一个关键的东东:
response.setCharacterEncoding("utf-8");//编码
response.getWriter().print(json.toString());//输出
------解决方案--------------------------------------------------------
Java code
response.getWriter().print(json.toString());//输出
------解决方案--------------------------------------------------------
后台品字符串成为JSON格式 然后使用AJAX返回到页面。
  相关解决方案