【发布时间】:2017-11-12 01:15:57
【问题描述】:
我已经尝试了这个站点上的所有内容,但没有任何效果,我只想从 servlet 生成的 JSON 中获取状态,然后使用 AJAX 在选择栏上显示数据。
但无论我在选择栏上键入什么内容,我都只会得到“未找到结果”。
这是填充下拉列表的函数:
注意:formatRepo 和 formatRepoSelection 已添加
$(document).ready(function() {
$(".js-data-example-ajax").select2({
ajax: {
url: "/socialis/estadoController",
dataType: 'json',
delay: 250,
data: function(params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function(data, params) {
// parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to
// alter the remote JSON data, except to indicate that infinite
// scrolling can be used
params.page = params.page || 1;
return {
results: data.items,
pagination: {
more: (params.page * 30) < data.total_count
}
};
},
cache: true
},
escapeMarkup: function(markup) {
return markup;
}, // let our custom formatter work
minimumInputLength: 1,
templateResult: formatRepo, // omitted for brevity, see the source of this page
templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
});
});
这是 Servlet doGet 方法:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String estados = new Gson().toJson(localizacionDao.getEstados());
response.getWriter().write(estados);
}
我通过每次在选择栏上键入时打印“estados”来验证 JSON 是否正确生成。
提前致谢。
【问题讨论】:
标签: jquery json ajax servlets jquery-select2