@ResponseBody //加上这个注解,前台会自动变成json格式的字符串
public List<Single> selectAll() {
List<Single> list = singleService.selectAll();
return list;
}
2、用普通方法
public List<Single> selectAll(HttpServletRequest request,HttpServletResponse response) throws IOException{
PrintWriter out = response.getWriter();
List<Single> list = singleService.selectAll();
String s = JSONArray.fromObject(list).toString();
out.write(s);
out.flush();
out.close();
return list;
}
前台用Ajax接受:
$(document).ready(function(){
var s = "../single/selectAll"; //url请求
$.ajax({
type:"POST",
url:s,
dataType:"json",
success:function(data, textStatus,jqXHL){
for(var o in data){
//用data[o].属性名可以取到值
alert("题目:"+data[o].QUESTIONSNAME+"A选项:"+data[o].A+"B选项:"+data[o].B+"C选项:"+data[o].C+"D选项:"+data[o].D+
"E选项:"+data[o].E+"F选项:"+data[o].F+"答案:"+data[o].ANSWER)
var myDate = new Date();
var date = myDate.getTime();
$("#singletable").append("<tr id='tr"+date+"'>"+data[o].QUESTIONSNAME+"</tr>");
}
},
error:function(){
alert("未接收到");
}
});
})