![](https://i-blog.csdnimg.cn/blog_migrate/b31fd9788a0478105e42980a6d13ce83.png)
前端html页面
<label class="layui-form-label">服务器环境</label>
<div class="layui-input-inline" style="width:150px;">
<select name="server" id="server">
<option value="">请选择服务器</option>
</select>
</div>
</div>
js脚本
$.ajax({
url:"/selectServer",
type:"GET",
dataType:"json",
success:function(result){
var list = result; //返回的数据
var server = document.getElementById("server"); //server为select定义的id
for(var p in list){
var option = document.createElement("option"); // 创建添加option属性
option.setAttribute("value",p); // 给option的value添加值
option.innerText=list[p]; // 打印option对应的纯文本
server.appendChild(option); //给select添加option子标签
form.render("select"); // 刷性select,显示出数据
} } });}
后端controller返回JSONObject给前端作显示处理,如:{"xx":"aaa","yy":"bbb"}
@RequestMapping("/selectServer")
public JSONObject selectServer(HttpServletRequest request){
List<Config> configContent = new ArrayList<Config>();
configContent = configService.getConfigList(null,9,null);
JSONObject ServerJson = new JSONObject();
for (Config c : configContent) {
ServerJson.put(c.getConfigkey(), c.getConfigContent());
}
log.info("request:selectServer, response:" + ServerJson);
return ServerJson;
}