1. 程式人生 > >ajax post傳送陣列以及java後臺接收陣列

ajax post傳送陣列以及java後臺接收陣列

1 需要的包

  1.1 jquery包

  2  json包 (  maven 配置 )

<dependency>
		    <groupId>net.sf.json-lib</groupId>
		    <artifactId>json-lib</artifactId>
		    <version>2.3</version>
		    <classifier>jdk15</classifier>
		 </dependency>  
2 前端ajax
var saveAllNbrs = function(){
   var indexArray = new Array();
   var nbrMacArray = new Array();
   var nbrTaArray = new Array();
   var nbrRssArray = new Array();
   for(var i = 0 ; i < 20 ; i++){
	   var nbrIndex = document.getElementById("nbrIndex"+i);
	   if(nbrIndex&&nbrIndex.value!=""){
		   var nbrMac = document.getElementById("nbrMac"+i).value;
		   var nbrTa = document.getElementById("nbrTa"+i).value;
		   var nbrRssiTrd = document.getElementById("nbrRssiTrd"+i).value;
		   indexArray.push(nbrIndex.value);
		   nbrMacArray.push(nbrMac);
		   nbrTaArray.push(nbrTa);
		   nbrRssArray.push(nbrRssiTrd);
	   }else{
		   break;
	   }
   }
   var capId = document.getElementById("capId").value;
   var saveAllNbrsUrl = getRootPath() +"/web/deviceConfig/deviceConfigController/updateNbrs.do";
   var data = {
//      'id':capId,
	  'indexArray':JSON.stringify(indexArray),
//	  'nbrMacArray':JSON.stringify(nbrMacArray),
//	  'nbrTaArray':JSON.stringify(nbrTaArray),
//	  'nbrRssArray':JSON.stringify(nbrRssArray)
	 };
   $ajax(saveAllNbrsUrl,"post",data,function(result){
		if(result){
			alert(result);
		}
	})
}

3 後臺java解析
	String id = request.getParameter("id");
	String index = request.getParameter("indexArray");
	String nbrMac = request.getParameter("nbrMacArray");
	String nbrTa = request.getParameter("nbrTaArray");
	String nbrRss = request.getParameter("nbrRssArray");
	List<Integer> indexList =  JSON.parseArray(index, Integer.class);