java 下拉框使用json_java - 需要从java返回一个json列表,使用json填充html中的下拉列表 - SO中文参考 - www.soinside.com...

我已经解决并发布了答案。我现在有另一个问题。这是我的第一个使用json,java和html的项目,所以有点学习曲线。

我使用相同的代码来提取一些列表(区域列表,区列表);但是,区列表会返回不同的结果/格式。 Region列表是正确的并返回:

eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS85c0FhWC5wbmcifQ==

我现在成功地使用它来填充下拉列表。然后,我选择其中一个值,并使用非常相似的代码重新调整所选区域内的区域列表。返回:

eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9nOGxXUy5wbmcifQ==

注意方括号和引号。

我使用的代码是:

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,它返回方括号和引号中的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值