I used JSONObject as shown below in Servlet.
JSONObject jsonReturn = new JSONObject(); NhAdminTree = AdminTasks.GetNeighborhoodTreeForNhAdministrator( connection, bwcon, userName); map = new HashMap<String, String>(); map.put("Status", "Success"); map.put("FailureReason", "None"); map.put("DataElements", "2"); jsonReturn = new JSONObject(); jsonReturn.accumulate("Header", map); List<String> list = new ArrayList<String>(); list.add(NhAdminTree); list.add(userName); jsonReturn.accumulate("Elements", list);
The servlet returns this JSON object, as shown below:
response.setContentType("application/json"); response.getWriter().write(jsonReturn.toString());
This servlet is invoked from a browser using AngularJs, as shown below.
$scope.GetNeighborhoodTreeUsingPost = function(){ alert("Clicked GetNeighborhoodTreeUsingPost : " + $scope.userName ); $http({ method: 'POST', url : 'http://localhost:8080/EPortal/xlEPortalService', headers: { 'Content-Type': 'application/json' }, data : { 'action': 64, 'userName' : $scope.userName } }).success(function(data, status, headers, config){ alert("DATA.header.status : " + data.Header.Status); alert("DATA.header.FailureReason : " + data.Header.FailureReason); alert("DATA.header.DataElements : " + data.Header.DataElements); alert("DATA.elements : " + data.Elements); }).error(function(data, status, headers, config) { alert(data + " : " + status + " : " + headers + " : " + config); }); };
This code worked, and it displays the correct data in the warning dialog:
Data.header.status: Success
Data.header.FailureReason: None
Data.header.DetailElements: 2
Data.Elements: string values ββseparated by coma, i.e. NhAdminTree, username
Rahul varadkar
source share