前台使用jquery的ajax查询数据
js代码:
$(function(){
$.ajax({
url:'findToAdmin.action',
type:'get',
data:{state:'admin'},
dataType:'json',
cache:false,
success:function(jsonData){
for(var i=0;i<jsonData.length;i++){
alert(jsonData[i].name);
}
},
error:function(){
}
});
});
java代码:
public String findToAdmin(){
String state=ServletActionContext.getRequest().getParameter("state");
Map<Object,Object> param=new HashMap<Object,Object>();
param.put("resType=?", state);

List<FunctionAuthority> authoritysList=functionAuthorityService.findToAdmin(param);

JSONArray jsonArray=new JSONArray();

for(int i=0;i<authoritysList.size();i++){

Map<Object,Object> map=new HashMap<Object,Object>();

map.put("id", authoritysList.get(i).getId());
map.put("name", authoritysList.get(i).getName());
map.put("parentId", authoritysList.get(i).getParentId());
map.put("resType", authoritysList.get(i).getResType());
map.put("descn", authoritysList.get(i).getDescn());

jsonArray.add(map);
}

String jsonStr=jsonArray.toString();
String jsonString="{jsonData:"+jsonStr+"}";
jsonUtils.outJson(jsonString);
return SUCCESS;
}

解决方案 »

  1.   

    这是outJson方法:
    public  void outJson(String xmlStr) {
    HttpServletResponse response = ServletActionContext.getResponse();
    response.setContentType("text/html;charset=utf-8");
    try {
    PrintWriter out = response.getWriter();
    out.write(xmlStr);
    out.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
      

  2.   

    在error里alert试试,没准是执行的流程错了,进error了
      

  3.   

    alert(XMLResponse.responseText)过了 直接就进了error 打出来的是后台传过来的json数据