xhr转json问题
// 定义上传对象
var xmlhttp;
// 显示确认登记单
function confirmModalBlock() {
var modal = document.getElementById('confirmModal');
// 获取勾选的id
var appointId;
var items = document.getElementsByName('appointId');
for (var i = 0; i < items.length; i++) {
if (items[i].checked) {
appointId = items[i].value;
break;
}
}
// 获取信息接口
if (appointId != undefined) {
// 路径
var url = "http://localhost:8080/getSampleAppoint?appointId=" + appointId;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = checkResult;
xmlhttp.open("GET", url, true);
// 发送的信息
xmlhttp.send(null);
modal.style.display = "block";
} else {
alert("请选择预约单!");
}
}
function checkResult() {
// 如果回传成功
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var result = xmlhttp.responseText;
var json = eval("(" + result + ")");
alert(json.data.companyName)
}
}
// 后端接口
@RequestMapping("getSampleAppoint")
public ModelAndView getSampleAppoint(String appointId) {
ModelAndView mav = new ModelAndView();
SampleAppoint sampleAppoint = sampleAppointService.getById(appointId);
JSONObject jsonObject = JSONObject.fromBean(sampleAppoint);
JSONObject res = new JSONObject();
res.put("data", jsonObject);
mav.addObject("message", res);
mav.setViewName("data");
return mav;
}