[英]how to retrieve the values from servlet to jsp using json
我是剛使用json和ajax的人。 請有人幫助我如何將值附加到選項標簽。 值將被打印在頁面本身上,而不是選項標簽內。 我看過很多例子,但看不懂。
檢索servlet:
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();
}
}
我的jsp代碼:
<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>
這段代碼可以正常工作並添加到下拉列表中,但這些值也會顯示在頁面頂部。有人可以給我解決方案嗎?
<script>
$(document).ready(function() {
$.get("Retrieveservlet", function(json) {
/* x = json[0];
alert(x); */
var myselect = $('#dropDownId');
$.each(json, function(index, key) {
myselect.append($('<option></option>').val(key).html(key));
});
//$('#dropDownId').append(myselect.html());
});
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.