ajax post传送数组以及java后台接收数组

1 需要的包

  1.1 jquery包

  2  json包 (  maven 配置 )

  


		    net.sf.json-lib
		    json-lib
		    2.3
		    jdk15
		   
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 indexList =  JSON.parseArray(index, Integer.class);




你可能感兴趣的:(ajax)