加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax和json一起使用

发布时间:2020-12-16 02:07:43 所属栏目:百科 来源:网络整理
导读:直接看代码 1.Java代码(利用的是阿里巴巴的json工具类"import com.alibaba.fastjson.JSON;") @RequestMapping("getmachineList") @ResponseBody public String validateAccount(@RequestParam(value = "storeId") Long storeId){ MapString,Object resultMap

直接看代码

1.Java代码(利用的是阿里巴巴的json工具类"import com.alibaba.fastjson.JSON;")

@RequestMapping("getmachineList")
@ResponseBody
public String validateAccount(@RequestParam(value = "storeId") Long storeId){
Map<String,Object> resultMap = new HashMap<String,Object>();
// 体验店机器
SimulatorQueryVo queryVo = new SimulatorQueryVo();
queryVo.setSiteId(storeId);
queryVo.setState(MachineStatusMap.used);
List<Simulator> simulatorList = simulatorService.queryList(queryVo).getModel();
Map<String,Object> map = new HashMap<String,Object>();
for(Simulator simulator:simulatorList)
{
map.put(simulator.getAlias(),simulator.getSerial());
}
resultMap.put("result",map);
String resultAll = JSON.toJSONString(resultMap);
return resultAll;
}


2.jsp页面中解析

//异步获取体验店仿真机
function getmachineList(){
$(".option").remove();
var storeId = $('#query-site').val();
$.ajax({
type: 'POST',
data: 'storeId=' +storeId,
url : '/admin/site/precontract/getmachineList',
dataType: 'json',
success:function(data){
var json = $.parseJSON(data);
var model = json.result;
var i = 0;
$.map(model,function(value,key) {
var alias = key;
var serial = value;
$("#query-machine").append('<option class="option" value="'+serial+'">'+alias+'</option>');
});
},
error:function(){
$.messager.alert("提示","加载失败!","info");
}
});
}


通过以上代码实现了动态显示级联下拉选项.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读