Ajax在springMVC中实现前后台交互(后台回集合,前台获取集合对象)
前台:
$.ajax({
url:"searchxxx.do",
type:"post",
dataType:"JSON",
async:"true",//异步请求
data:{"name":setTname,"id":1},
success:function(data){
//alert(data)
$.each(data, function(i,item){ // i:获取对象的下标 ,item:获取对象的值
alert(item.tribeid+","+item.tribename);
});
},
error:function (){
alert("错误")
}
});
后台:
List<> list=new ArrayList<>();
list=......;//查询出对象集合存入list
JSONArray obj = JSONArray.fromObject(list);
return obj.toString();
为防止在jsp页面中出现中文乱码,在Controller中的requestMapping中加 produces = "text/json;charset=UTF-8"
@RequestMapping(value = "manager/searchTribeLikeName", produces = "text/json;charset=UTF-8")
@ResponseBody