I'm new to use json and ajax. Please someone help me on how to append the values to the option tag. The values are getting printed on the page itself than inside the option tag. I have seen so many examples but do not understand.
Retrieveservlet :
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("action is called inside dropdown");
ViewValuesInDB daoobj = new ViewValuesInDB();
ArrayList<String> list = new ArrayList<String>();
String json = null;
try {
list = daoobj.makeConnection();
System.out.println(list);
if(null == list){
System.out.println("list is null");
}
else{
System.out.println("list has values");
}
System.out.println("size of ids list :"+list.size());
/*using json*/
json = new Gson().toJson(list);
System.out.println(json);
response.setContentType("application/json");
response.getWriter().write(json);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
my jsp code :
<script>
$(document).ready(function() {
$.get("Retrieveservlet", function(responseJson) {
('#dropDownId').append($('<option>').text(value)('value'));
});
});
</script>
<body>
<label for="dropDownId">Country*:</label>
<select id="dropDownId">
</select>
</body>