Spring MVC3下Ajax传值的一个完整实例

//前台代码

var idsCheckedCheck = $(".check:checked");//选中check了的所有的复选框

var timestamp = Date.parse(new Date());//时间戳,用于从服务器得到最新的数据

$.ajax({
 type : 'GET',
 contentType : 'application/json',
 url: '${ctx}/deleteAppointment/'+idsCheckedCheck.serialize()+'/'+timestamp,
 dataType : 'text',
 beforeSend: function(data) {},
 success: function(data) {
                    data=eval("("+data+")");//获取从后台返回的数据,通常是Json格式
      if (data) {
                  alert(data.num);

                         }else{
    }
    }
   });

<body>

<form action="#" method="get" id="listForm">

<input type="checkbox" name="ids" value="${eachAppointment.appid}" class="check" />//name属性一定要有

<input type="checkbox" name="ids" value="${eachAppointment.appid}" class="check" />

</form>

</body>

 

//后台代码


@RequestMapping(value = "/deleteAppointment/{idsCheckedCheck}/*", method = RequestMethod.GET) 
    @ResponseBody 
    public void deleteAppointment(@PathVariable("idsCheckedCheck") String idsCheckedCheck,HttpServletResponse response,HttpServletRequest request) {

      String[] ids=idsCheckedCheck.split("ids\\=|\\&ids\\=");//解析从前台获得的数据

      int num=0;

      for(int i=1;i<ids.length;i++){
              num++;
}

     //从下标1开始访问ids中的内容
      response.setContentType("text/Xml;charset=gbk");  
      PrintWriter out = null;
      try {  
          out = response.getWriter();
          String json="{\"num\":"+num+"}";//拼成Json格式字符串
          out.println(json);        
      }  
      catch (IOException ex1) {  
          ex1.printStackTrace();  
      }finally{  
                             out.close();  
                    }
    }

你可能感兴趣的:(spring,Ajax,序列化,MVC3)