日期:2014-05-17  浏览次数:20588 次

求助JSON,AJAX
Java code


//这个是根据ID查询出来的一个Name
public 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返回到页面。