出现这样的问题,可能是json数据转化格式有问题
我用的是response参数
所以我的后台数据代码如下:
private FootWorker footWorker=new FootWorker();
private FootWorkerDao footWorkerDao=new FootWorkerDao();
public String findWorkerByWname() {
Map<String, Object> map = new HashMap<String, Object>();
PageBean pageBean=new PageBean();
pageBean.setRequest(request);
try {
List<Map<String, Object>> list=footWorkerDao.findWorkerByWname(request.getParameterMap(), pageBean);
//以下code,total,data在前端js代码中写一致的字段
map.put("code", 0);
map.put("total", pageBean.getTotal());
map.put("data", list);
//System.out.println(list.size());
ObjectMapper objectMapper=new ObjectMapper();
ResponseUtil.write(response, objectMapper.writeValueAsString(map));
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
ResponseUtil代码:
package com.hsl.util;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
public class ResponseUtil {
public static void write(HttpServletResponse response,Object o)throws Exception{
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println(o.toString());
out.flush();
out.close();
}
}
前台js代码如下:
layui.use(['layer', 'form','table'], function(){
var layer = layui.layer
,form = layui.form
,table = layui.table;
var ip=document.getElementById("ip").value;
table.render({
elem:'#wtb'//表格id
,url:ip+'/sy/worker_findWorkerByWname.action'//所对应调用的接口
,method:'post'
,page:'true'//分页
, id: 'idTest'
,response: {//
code:'code',
total:'total',
data:'data'
}
,cols: [[ //标题栏
{type: 'checkbox', fixed: 'left'}
,{field: 'wid', title: '员工编号', width: 110, sort:true,fixed:true}
,{field: 'wname', title: '员工姓名',width: 110}
,{field: 'wsex', title: '员工性别', width: 110}
,{field: 'wage', title: '员工年龄', width: 110}
,{field: 'wnum', title: '员工电话', width: 110}
,{field: 'wjob', title: '员工职位', width: 110}
,{field: 'wsalary', title: '员工薪资', width: 110}
,{fixed: 'right', title:'操作', width: 110, align:'center', toolbar: '#barDemo'}
]]
});
// console.log(11);
layer.msg('欢迎来到食居后台系统');
});
结果如下: