I am new to Ajax and would have thought there would be plenty of examples of passing an array from java to ajax; however, I have not been able to find them. I want to pass three strings to ajax and then display them in HTML. My java code is:
System.out.println("Authenticated");
String json = new Gson().toJson(questionList);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
The ajax is:
dataType: "json";
alert(responseJson);
var result = $.parseJSON(responseJson); <-- this line fails
$('#question1').val(result.question1);
$('#question2').val(result.question2);
$('#question3').val(result.question3);
The alert displays "Question 1 is a?,Question 2 is b?,Question 3 is c?".
How do I pass each of the strings to display in the HTML. I suspect I need to build the array in java differently as well as receive the result in ajax differently; however, I can not find an applicable example. I do not want to use unstring as the question could contain "," or other delimiter used.
jsondo you get afterString json = new Gson().toJson(questionList);? Because from the alert displays, it seems not a valid json string.