猿问

如何使用 Ajax 将 Array 值从 Javascript 格式化为 Java Servlet

我需要你的帮助。


我使用 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?


一只萌萌小番薯
浏览 134回答 1
1回答

心有法竹

不要字符串化只是保持原样&nbsp;data: { BuildingNo : BuildingNo }在 Java Servlet 中&nbsp;List <String> oldBuildingList = Arrays.asList(request.getParameterValues("BuildingNo[]");
随时随地看视频慕课网APP

相关分类

Java
我要回答