1 在control层数据转换成json对象的代码如下: 2 3 @RequestMapping("/getMyPlan") 4 public @ResponseBody Map getMyPlan(Integer idKey)throws Exception{ 6 Map josnMap = new HashMap(); 8 List tasks =projectManagementService.findTaskByProjectId(idKey); 10 List list=new ArrayList(); 13 if(tasks.size()>0){ 14 for(Task task:tasks){ 17 Map taskMap=new 18 HashMap(); 19 taskMap.put("id",task.getId()); 21 taskMap.put("specification", 22 task.getSpecification());
list.add(taskMap); 24 } 25 }else{ 27 Map taskMap=new HashMap(); 29 taskMap.put("id",0); 31 taskMap.put("specification", null); 33 list.add(taskMap); 34 } 35 josnMap.put("tasks",list); 36 return 37 josnMap; 38 } 39 40 前端的js中ajax的代码如下: 41 42 $(function() 43 { 44 //ajax给活动计划赋值------------------------------------------------------ 45 46 $("#activity_project").change(function(){ 47 48 var pro=$("#activity_project").val(); 49 50 $.ajax({ 51 type: 52 "POST", 53 url: 54 "${pageContext.request.contextPath}/getMyPlan", 55 56 data: { //发送给数据库的数据 57 58 idKey:pro 59 60 }, 61 62 dataType: 'json', 63 success: 64 function(data) { 65 66 $.each(data.tasks, 67 function(index,item){ 68 69 70 $("#activity_taskid").empty(); 71 72 73 $("#activity_taskid").append(' 74 75 '+item.specification+'') 76 77 }); 78 79 } 80 }) 81 82 }); 83 84 //-------------------------------------结束------------------------------------ 85 });
最后