@ResponseBody 将对象转换成json数据
例子
@RequestMapping("/queryArea")
@ResponseBody
public List<Area> queryArea(HttpServletRequest request, HttpServletResponse response) {
List<Area> areaList = saleService.queryAreaByParentId("0"); // 查询父类id
request.setAttribute("areaList", areaList);
System.out.println("进入queryArea");
return areaList;
}
前端
$.ajax({
type : "POST",
url : "sale/queryArea",
dataType : "json",
success : function(data) {
console.log(data)
if (data != null && data.length > 0) {
var html2 = "";
//判断情况 生成职位 为空不生成职位
html += "<option value=''>---请选择区域---</option>";
for ( var i = 0; i < data.length; i++) {
html += "<option value='"+data[i].dictcode+"'>"
+ data[i].dictname
+ "</option>";
}
}
console.log(html2)
$("#areaList").html(html2);
}
})