文中的LayUIAdminPro版本为v2020.4.1。使用的软件是IDEA,你问我为什么不用vsCode?为了偷懒,就使用IDEA。
在使用当我们的成功码和LayUI的成功码不一致时,会出现下图这种错误。
下面是一个table.render请求,这样去请求的话,会报上图这种错误。
table.render({
elem: '#LAY-stream-user-manage',
url: 'http://localhost:8001/api/user/find',
method: "post",
contentType: "application/json;charset=UTF-8",
dataType: "json",
where: {
'type': 'user'
},
cols: [[
{type: 'checkbox', fixed: 'left'},
{field: 'id', title: 'User_id', templet: '#LAY-user-id'},
{field: 'nickname', title: 'User_name'},
{field: 'email', title: 'User_email'},
{field: 'loginTime', title: 'User_last_login_at', sort: true},
{field: 'enabled', title: 'User_status', templet: '#LAY-user-status'},
{title: 'Action', align: 'center', fixed: 'right', toolbar: '#table-content-list'}
]],
autoSort: false,
skin: "line",
page: {limit: 10, sort: 'id', desc: false, curr: 1, groups: 3},
limit: 10,
limits: [10, 15, 20, 25, 30],
text: {none: '查无数据'},
});
解决办法是在table.render中加上parseData属性,在函数中解析状态码。
table.render({
elem: '#LAY-stream-user-manage',
url: 'http://localhost:8001/api/user/find',
method: "post",
contentType: "application/json;charset=UTF-8",
dataType: "json",
where: {
'type': 'user'
},
cols: [[
{type: 'checkbox', fixed: 'left'},
{field: 'id', title: 'User_id', templet: '#LAY-user-id'},
{field: 'nickname', title: 'User_name'},
{field: 'email', title: 'User_email'},
{field: 'loginTime', title: 'User_last_login_at', sort: true},
{field: 'enabled', title: 'User_status', templet: '#LAY-user-status'},
{title: 'Action', align: 'center', fixed: 'right', toolbar: '#table-content-list'}
]],
autoSort: false,
skin: "line",
page: {limit: 10, sort: 'id', desc: false, curr: 1, groups: 3},
limit: 10,
limits: [10, 15, 20, 25, 30],
text: {none: '查无数据'},
parseData: function (res) { //res 即为原始返回的数据
// 解析响应结果
return {
"code": 0 //解析接口状态
}
}
});