后台方法走断点一直到action中return success都可以取到值,但是前台一直取到null,小弟不才,附上案例,求大侠分析分析。
取值的方法。
public List<JsonTreeNode> AcRoleTreeJson(AcRole role) {
List<JsonTreeNode> JsonTreeNodeList = new ArrayList<JsonTreeNode>();
List<AcRole> organizationList = null;
if (role == null ||role.getAcApplication().getAppid().equals("")
|| role.getAcApplication().getAppid() == null) {
organizationList = this.findByProperty(AcRole.class, "acApplication.appid", "4028813c3d3f5f44013d3f5fdc810000");
}else if(role != null) {
organizationList = this.findByProperty(AcRole.class, "acApplication.appid", "4028813c3d3f5f44013d3f5fdc810000");
}
if (organizationList != null ) {
for (int i = 0; i < organizationList.size(); i++) {
AcRole role2 =  organizationList.get(i);
JsonTreeNode jsonTreeNode = new JsonTreeNode();
List<AcRole> subOrgList = this.findByProperty(AcRole.class,
"acApplication.appid", "4028813c3d3f5f44013d3f5fdc810000");
if (subOrgList != null && subOrgList.size() > 0) {
jsonTreeNode.setExpandable(true);
// 当前级类型
jsonTreeNode.setType("org");
// 是否叶子节点
jsonTreeNode.setLeaf(false);
} else {
// 当前级类型
jsonTreeNode.setType("org_last");
// 是否叶子节点
jsonTreeNode.setLeaf(true);
}
// 设置ID
jsonTreeNode.setId(role2.getRoleid());
// 设置显示名称
jsonTreeNode.setText(role2.getRolename());
// 设置显示样式
jsonTreeNode.setCls("folder");
// 是否展开
jsonTreeNode.setExpandable(true); JsonTreeNodeList.add(jsonTreeNode);
}
}  
return JsonTreeNodeList;
}
action中的方法
public String AcRoleTreeJson()  throws IOException{
try {
jsonTreeNodeList = this.roleBusiness.AcRoleTreeJson(acRole);
} catch (RuntimeException e) {
e.printStackTrace();
}
return SUCCESS; 
}
struts.xml
<action name="AcRoleTreeJson" class="roleAction" method="AcRoleTreeJson">
<result name="success" type="json">
<param name="root">jsonTreeNodeList</param>
</result></action>最后是前台取值的ajax
jQuery.ajax({
type:'post',
url:'AcRoleTreeJson.do',
success:function(result){ 
//alert(result);
alert(result);
var data = result;
var selectEle = jQuery("<select style='height:20px;margin-top:5px;' ></select>").appendTo(jQuery("#role")); 
jQuery("<option>请选择</option>").val("").appendTo(jQuery(selectEle));
jQuery.each(data,function(i,item){
jQuery("<option></option>").val(item["id"]).text(item["text"]).appendTo(jQuery(selectEle));
})
}
})  

前台就是一直取不到东西jsonactionajax

解决方案 »

  1.   

    PrintWriter out = response.getWriter();
    out.write(str);
    out.flush();
    out.close();在action中加上以上代码  str为你要传到前台的值
      

  2.   

    success:function(result){ 
                //alert(result);
                alert(result);
                var data = result;
                var selectEle = jQuery("<select style='height:20px;margin-top:5px;' ></select>").appendTo(jQuery("#role")); 
                jQuery("<option>请选择</option>").val("").appendTo(jQuery(selectEle));
                jQuery.each(data,function(i,item){
                    jQuery("<option></option>").val(item["id"]).text(item["text"]).appendTo(jQuery(selectEle));
                }
    这里的alert有值吗?
      

  3.   

    $.ajax中type:'POST'大写,加入dataType:'json'
      

  4.   

    看看吧 http://blog.sina.com.cn/s/blog_a3869e9a01014wnu.html
      

  5.   

    $.ajax({
          type:"POST", 
          url:urlStr,//路径
          data:dt, //传入参数
          dataType:"json", //数据类型
          success:function (ret){}
    可以参考这个格式试试。希望对你有用