简体   繁体   中英

How to format Array value from Javascript to Java Servlet using Ajax

I am in need of your help.

I have this array being passed from Javascript to Java Servlet using AJAX.

Javascript and AJAX snippet code:

var BuildingNo = [];

$(xml).find('BUILDING').each(function(){
    BuildingNo.push($.trim(this).children('BuildingNo').text());
}

The value of BuildingNo before passing to Java Servlet.

alert(BuildingNo);
// 00101,00102,00103,00104,00105 

The value of JSON.stringify(BuildingNo) when I passed the BuildingNo through Ajax to Java Servlet.

data: { BuildingNo : JSON.stringify(BuildingNo) },

alert(JSON.stringify(BuildingNo));
// ["00101","00102","00103","00104","00105"] 

Here is the Java Servlet snippet code I am using to pass the BuildingNo to oldBuilding list.

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.

}

How do I get the same format of newBuildingList using oldBuildingList?

Don't stringify just keep as data: { BuildingNo : BuildingNo }

and in Java Servlet List <String> oldBuildingList = Arrays.asList(request.getParameterValues("BuildingNo[]");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM