我需要你的帮助。
我使用AJAX将这个数组从Javascript传递到Java Servlet。
Javascript和AJAX代码段代码:
var BuildingNo = [];
$(xml).find('BUILDING').each(function(){
BuildingNo.push($.trim(this).children('BuildingNo').text());
}
传递给Java Servlet之前BuildingNo的值。
alert(BuildingNo);
// 00101,00102,00103,00104,00105
当我通过Ajax将BuildingNo传递给Java Servlet时,JSON.stringify(BuildingNo)的值。
data: { BuildingNo : JSON.stringify(BuildingNo) },
alert(JSON.stringify(BuildingNo));
// ["00101","00102","00103","00104","00105"]
这是我用来将BuildingNo传递到oldBuilding列表的Java Servlet代码段。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws (ServletException, IOException {
List <String> oldBuildingList = Arrays.asList(request.getParameter("BuildingNo");
logger.info(oldBuildingList);
// [["00101","00102","00103","00104","00105"]] -- This is a wrong format.
//I have a Java code here which captures a new list of BuildingNo from another service.
List <String> newBuildingList = new ArrayList <String>();
logger.info(newBuildingList.toString());
// [00106,00107,00108,00109,00110] -- This should be the correct format of oldBuildingList.
}
如何使用oldBuildingList获得相同格式的newBuildingList?
最佳答案
不要只保留为data: { BuildingNo : BuildingNo }
并且在Java Servlet List <String> oldBuildingList = Arrays.asList(request.getParameterValues("BuildingNo[]");
中