我已经解决并发布了答案。我现在有另一个问题。这是我的第一个使用json,java和html的项目,所以有点学习曲线。
我使用相同的代码来提取一些列表(区域列表,区列表);但是,区列表会返回不同的结果/格式。 Region列表是正确的并返回:
我现在成功地使用它来填充下拉列表。然后,我选择其中一个值,并使用非常相似的代码重新调整所选区域内的区域列表。返回:
注意方括号和引号。
我使用的代码是:
HTML:
Select Region:
Select your State first
Select District:
Select your State first
JSON:
$(document).ready(function(){
$('#selectState').on('change', function() {
$.ajax({
type: "POST",
url: "RegionView",
cache: false,
data: $(selectState).serialize(),
success: function(data){
$('#ajaxGetUserServletResponse').text(data);
}
}).done(function(responseJson) {
dataType: "json",
// Clear options
$("#selectRegion").find("option").remove();
$('#selectRegion').append($('Select your Region'));
$("#selectDistrict").find("option").remove();
$('#selectDistrict').append($('Select your Region first'));
$("#selectGroup").find("option").remove();
$("#selectSection").find("option").remove();
$("#selectSubSection").find("option").remove();
// Loop through JSON response to populate the Region drop down
$.each(responseJson, function(key, value) {
$('').text(value).appendTo($("#selectRegion"));
});
});
});
$('#selectRegion').on('change', function() {
$.ajax({
type: "POST",
url: "DistrictView",
cache: false,
data: $(selectRegion).serialize(),
success: function(data){
$('#ajaxGetUserServletResponse').text(data);
}
}).done(function(responseJson2) {
dataType: "json",
// Clear options
$("#selectDistrict").find("option").remove();
$('#selectDistrict').append($('Select your District'));
$("#selectGroup").find("option").remove();
$('#selectGroup').append($('Select your District first'));
$("#selectSection").find("option").remove();
$("#selectSubSection").find("option").remove();
// Loop through JSON response to populate the District drop down
alert("Here1");
$.each(responseJson2, function(key, value) {
alert("Here2");
$('').text(value).appendTo($("#selectDistrict"));
});
});
});
$('#selectDistrict').on('change', function() {
$.ajax({
type: "POST",
url: "GroupView",
cache: false,
data: $(selectDistrict).serialize(),
success: function(data){
$('#ajaxGetUserServletResponse').text(data);
}
}).done(function(responseJson) {
dataType: "json",
// Clear options
$("#selectGroup").find("option").remove();
$('#selectGroup').append($('Select your Group'));
$("#selectSection").find("option").remove();
$('#selectSection').append($('Select your Group first'));
$("#selectSubSection").find("option").remove();
// Loop through JSON response to populate the Group drop down
$.each(responseJson, function(key, value) {
$('').text(value).appendTo($("#selectGroup"));
});
});
});
});
区域的Java(读取选定的状态并返回填充到下拉列表中的区域列表):
@WebServlet("/RegionView")
public class RegionView extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String state = request.getParameter("selectState"); // From bootstrap
MySQLConnection.getConnection();
List listRegions = MySQLConnection.listGroupRegions(state);
if (listRegions == null || listRegions.isEmpty()) {
response.getWriter().write("No Regions in this State.");
}else{
String json = new Gson().toJson(listRegions);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}
}
}
然后我选择一个Region并将它传递给这个java,它返回一个Districts列表。但是找到了区域,但返回的格式不正确,因此下一个下拉列表不会填充此列表:
@WebServlet("/DistrictView")
public class DistrictView extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] region = request.getParameterValues("selectRegion"); // From bootstrap
String region0 = region[0];
MySQLConnection.getConnection();
List listDistricts = MySQLConnection.listGroupDistricts(region0);
if (listDistricts == null || listDistricts.isEmpty()) {
response.getWriter().write("No Districts in this Region.");
}else{
String json = new Gson().toJson(listDistricts);
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}
}
}
我想将此列表返回给json并填充下拉列表,以便我可以选择一个区域并返回该区域中的组列表。然而;即使我转换为json,它返回方括号和引号中的值。