素材巴巴 > 程序开发 >

前端页面发ajax请求,ajax请求,json数据显示在了前端页面上。

程序开发 2023-09-03 20:18:59

1460000022659548

为啥我的json串直接显示在了前端页面上啊

而且断点调试没有给data赋值就输出了,data里面还有值。

jsp代码

function dologin() {

var floginacct = $("#floginacct");

var fuserpswd = $("#fuserpswd");

var ftype = $("#ftype");

var flag = $("#remberme")[0].checked;

//对于表单而言,不能用null进行判断,如果文本框什么都不输入,获取的值为“”

if($.trim(floginacct.val())=="") {

/*alert("用户账户不能为空,请重新输入!");*/

layer.msg("用户账号不能为空,请重新输入!", {time:1000, icon:5, shift:6}, function(){

floginacct.val("");

floginacct.focus();//设置指定元素获取焦点

//return false ; //不能结束if,只是结束msg函数.

});

/*floginacct.focus();

floginacct.val("");*/

return false;

}

$.ajax({

type : "POST",

data : {

"loginacct" : floginacct.val(),

"userpswd" : fuserpswd.val(),

"type" : ftype.val(),

"remberme" : flag?"1":"0"

},

/*dataType: "json",

async : false,*/

url : "${APP_PATH}/doLogin.do",

beforeSend : function () {

layer.msg("处理中", {icon:16});

//一般做表单数据校验

},

success : function (ajaxResult) { //{"success":true} 或 {"success":false,"message":"登录失败!!!"}

if (ajaxResult.success){

var data = ajaxResult.data;

var url = location.href;

var mark = url.substring(url.indexOf("?")+1,url.length);

if ("member"==data){

if(mark=="mark=1"){

window.location.href="${APP_PATH}/project/start.htm";

}else

window.location.href="${APP_PATH}/member/member.htm";

}else if("user"==data){

window.location.href="${APP_PATH}/main.htm";

}else{

layer.msg("登录类型不合法!", {time:1000, icon:5, shift:6});

}

} else{

/*alert(ajaxResult.success);*/

layer.msg("登录失败!", {time:1000, icon:5, shift:6});

}

},

error : function () {

alert("error");

}

});

$("#loginForm").submit();

/*var type = $(":selected").val();

if ( type == "user" ) {

window.location.href = "main.html";

} else {

window.location.href = "index.html";

}*/

}

controller的代码

@RequestMapping("/doLogin")

public AjaxResult doLogin(String loginacct, String userpswd, String type, String remberme, HttpSession session, HttpServletResponse response){

AjaxResult ajaxResult = new AjaxResult();

try {

Map paramMap = new HashMap();

response.setContentType("text/html;charset=utf-8");

paramMap.put("loginacct", loginacct);

paramMap.put("userpswd",MD5Util.digest(userpswd));

System.out.println(MD5Util.digest(userpswd));

paramMap.put("type", type);

if("member".equals(type)){

Member member = memberService.queryMemeberlogin(paramMap);

if(member!=null){

ajaxResult.setSuccess(true);

}else{

ajaxResult.setSuccess(false);

}

System.out.println(member);

session.setAttribute(Const.LOGIN_MEMBER,member);

if ("1".equals(remberme)){

//"loginacct=xxxxx&userpswd=xxxxxx&logintype=member"

String logincode = ""loginacct="+member.getLoginacct()+"&userpswd="+member.getUserpswd()+"&logintype=member"";

System.out.println("用户存放到Cookie中的键值对:logincode:::::::"+logincode);

Cookie c = new Cookie("logincode",logincode);

c.setMaxAge(60*60*24*14);

//如果设置为“/”,则本域名下contextPath都可以访问该Cookie。注意最后一个字符必须为“/”

c.setPath("/");

response.addCookie(c);

}

}else if("user".equals(type)){

User user = userService.queryUserlogin(paramMap);

session.setAttribute(Const.LOGIN_USER, user);

if(user!=null){

ajaxResult.setSuccess(true);

}

if ("1".equals(remberme)){

String logincode = ""loginacct="+user.getLoginacct()+"&userpswd="+user.getUserpswd()+"&logintype=user"";

System.out.println("用户存放到Cookie中的键值对:logincode:::::::"+logincode);

Cookie c = new Cookie("logincode",logincode);

c.setMaxAge(60*60*24*14);

c.setPath("/");

response.addCookie(c);

}

//---------------------------------------------------------------

//加载当前登录用户的所拥有的许可权限.

/*User user = (User)session.getAttribute(Const.LOGIN_USER);*/

List myPermissions = userService.queryPermissionByUserid(user.getId());

Permission permissionRoot = null;

Map map = new HashMap();

Set myUrls = new HashSet();//用于拦截器拦截许可权限

for (Permission innerpermission : myPermissions) {

map.put(innerpermission.getId(), innerpermission);

myUrls.add("/"+innerpermission.getUrl());

}

session.setAttribute(Const.MY_URIS,myUrls);

for (Permission permission : myPermissions) {

//通过子查找父

//子菜单

Permission child = permission ; //假设为子菜单

if(child.getPid() == null ){

permissionRoot = permission;

}else{

//父节点

Permission parent = map.get(child.getPid());

parent.getChildren().add(child);

}

}

session.setAttribute("permissionRoot", permissionRoot);

//---------------------------------------------------------------

}else{

}

ajaxResult.setData(type);

//返回的json串为 {"success":true}

}catch (Exception e){

ajaxResult.setMessage("登录失败!!!");

/*e.printStackTrace();*/

ajaxResult.setSuccess(false);

//返回的json串为 {"success":false,"message":"登录失败!!!"}

}

return ajaxResult;

}


标签:

上一篇: OpenStack 性能测试Rally 下一篇:
素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。