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

Rago

New Member
#1
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:
Mã:
var BuildingNo = [];

$(xml).find('BUILDING').each(function(){
    BuildingNo.push($.trim(this).children('BuildingNo').text());
}
The value of BuildingNo before passing to Java Servlet.
Mã:
alert(BuildingNo);
// 00101,00102,00103,00104,00105
The value of JSON.stringify(BuildingNo) when I passed the BuildingNo through Ajax to Java Servlet.
Mã:
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.
Mã:
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?
 

Admin

Administrator
Thành viên BQT
#2
Don't stringify just keep as data: { BuildingNo : BuildingNo }

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

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top