jquery ajax后台向前台传list 前台用jquery $.each遍历list
下边是两种前台接收并遍历list
$.ajax({ type: 'post', url: xxx.action', dataType: 'text', success: function(data){ var dataObj=eval("("+data+")"); for(var i=0;i<dataObj.length;i++){ alert(dataObj[i].id+" "+dataObj[i].name); } var jsonObj=eval("("+data+")"); $.each(jsonObj, function (i, item) { alert(item.id + "," + item.name); }); }, error: function(text) {} });
下边是后台接收并遍历list
HttpServletResponse res = ServletActionContext.getResponse(); res.reset(); res.setContentType("text/html;charset=utf-8"); PrintWriter pw = res.getWriter(); xxx.setId(1); xxx.setName("黑色头发"); list.add(xxx); xxx.setId(2); xxx.setName("紫色头发"); list.add(xxx); String json = JSONArray.fromObject(list).toString(); pw.print(json); pw.flush(); pw.close();