The following method is used in the controller:
@RequestMapping(value = "channelIntentionDetails.html", method = RequestMethod.POST)
public @ResponseBody
Report getChannelIntentionDetails(@RequestBody SearchParameters searchParameters) {
LOGGER.info("In ReportController.getChannelIntentionDetails(...), searchParameters " + searchParameters);
return channelDetailsService.getIntentionDetails(searchParameters);
}
Here is the structure of the Report POJO:
public class Report {
private Map<String, Collection<String>> parameterWiseResult;
private Collection<String> results;
private String result;
private String spid;
.
.
.
}
The results collection contains JSON strings retrieved from a MongoDB collection
This snippet shows the AJAX implementation in JavaScript:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","channelIntentionDetails.html",false);
xmlhttp.setRequestHeader("Content-Type","application/json");
xmlhttp.setRequestHeader("Accept","application/json");
xmlhttp.send(stringifiedSearchParameter);
alert("Plain AJAX response "+ xmlhttp.response);
alert("After JSON.parse(...) "+ (JSON.parse(xmlhttp.response)).results);
drawIntentionPieChart((JSON.parse(xmlhttp.response)).results);
function drawIntentionPieChart(data) {
if (intentionGooglePieChart == null
|| intentionGooglePieChart == 'undefined') {
intentionGooglePieChart = new google.visualization.PieChart(
document.getElementById('intentionPiechart'));
}
intentionGooglePieChart.clearChart();
var jsonData = data;
var data = new google.visualization.DataTable();
data.addColumn('string', 'Intention');
data.addColumn('number', 'Share');
for (i = 0; i < jsonData.length; i++) {
data.addRows([ [ jsonData[i]._id.Intention_category,
parseInt(jsonData[i].count) ] ]);
}
var options = {
title : 'Intention Analysis',
titleTextStyle : {
color : '#0E5EAE'
},
fontSize : 14,
width : 390,
height : 200
};
intentionGooglePieChart.draw(data, options);
}
The first alert displays the content where "results" is an array:
Plain AJAX response {"parameterWiseResult":null,"results":["{ \"_id\" : { \"SpId\" : 352 , \"Intention_category\" : \"Opine\" , \"Report_Id\" : 2 , \"Channel_Id\" : 1} , \"count\" : 1}",...],"result":null,"spid":null,"idvallistsearchprofile":null,"idvallisttags":null,"spmaster":null,"competitiveParameters":null}
The second alert strips off the array braces after JSON parsing:
After JSON.parse(...) { "_id" : { "SpId" : 352 , "Intention_category" : "Opine" , "Report_Id" : 2 , "Channel_Id" : 1} , "count" : 1},...
Subsequently, there are issues encountered while iterating over this parsed result:
TypeError: jsonData[i]._id is undefined
data.addRows([ [ jsonData[i]._id.Intention_category,
parseInt(jsonData[i].count) ] ]);
What am I doing wrong here?