JSONObject jsonObject = new JSONObject();
if(byEntity == null){
jsonObject.put("res", 0);
}else{
request.getSession().setAttribute("role", 3);
jsonObject.put("res", 1);
}
}else{
}
return jsonObject.toString();
form.on('submit(login)', function(data) {
$.ajax({
type : "post",
url : "${ctx}/login/toLogin",
data : data.field,
dataType:"json",
success : function(data) {
var res = data.res;
if (res == 0){
layer.msg('用户名或者密码错误!');
}else if (res == 2){
layer.msg(data.msg);
}else{
layer.msg('登录成功!');
window.location.href = ctx+"/login/index"
}
},
应该是返回你创建的jsonObject这个对象的字符串
jsonObject是一个类型为JSONObject 的对象
它可以用来生成或解析json
jsonObject.toString()就是把json对象转换成字符串对象的函数
在ajax中success,输出接收的参数data
form.on('submit(login)', function (data) {
$.ajax({
type: "post",
url: "${ctx}/login/toLogin",
data: data.field,
dataType: "json",
success: function (data) {
console.log('data', data)
var res = data.res;
if (res == 0) {
layer.msg('用户名或者密码错误!');
} else if (res == 2) {
layer.msg(data.msg);
} else {
layer.msg('登录成功!');
window.location.href = ctx + "/login/index"
}
}
})
})