1.页面ajax代码,使用$.ajax,获得json对象后each
$.ajax({ type:"GET", url:ctx + "/uploadImg.do", data:{method:'getPhoto',optCode:czydm.val()}, success:function(data){ var ary = eval(data); $(ary).each(function(i, e){ var idI =picTotalPages(); $("#imgDiv9").append(createImgDiv(e.photopath,idI)); var fileNameList = $("#fileList").val(); fileNameList = fileNameList + ";" + e.photopath+";CloseWindow"; $("#fileList").val(fileNameList); //总页数重新计算 picTotalPages(); }); }}
2.后台strut1代码
组成json格式的数据输出
json格式:
对象 {"id":"1","name":"zhangsan"}
数组 [{"id":"1","name":"zhangsan"},{"id":"2","name":"lisi"}]
public ActionForward getPhoto(ActionMapping mapping , ActionForm form , HttpServletRequest request , HttpServletResponse response) throws Exception { String optCode = request.getParameter("optCode"); UploadImgDao dao = new UploadImgDao(); List<HashMap> rs = dao.getPhotoPathByOptCode(optCode); String webRealRoot = PathUrlUtil.getWebRealRoot(request); String paths = "["; for (int i = 0; i < rs.size(); i++) { Map data = rs.get(i); String str = "{\"tid\":\"" + data.get("TID") + "\",\"photopath\":\"" + webRealRoot + data.get("PHOTOPATH")+ "\"}"; paths += str; if (i < rs.size() - 1) paths += ","; } paths += "]"; response.getWriter().print(paths); return null; }