首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

JSON,AJAX

2012-04-19 
求助JSON,AJAXJava code//这个是根据ID查询出来的一个Namepublic ActionForward getNameBySID(ActionMappi

求助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返回到页面。

热点排行