![](/img/trans.png)
[英]How to send byte array from javascript to java servlet through ajax
[英]How to format Array value from Javascript to Java Servlet using Ajax
我需要你的幫助。
我使用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 }
字符串data: { BuildingNo : BuildingNo }
並且在Java Servlet List <String> oldBuildingList = Arrays.asList(request.getParameterValues("BuildingNo[]");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.