前台:
$.ajax({
url:'LoginAction_toLogin.jspx',
type: 'post', dataType:'json', cache: false, async:false,
data: {"username":username, "password":password},
success: function(result){
if(result["flag"] == '0'){
window.location.href = "MenuAction_toInfo.jspx";
}else if(result["flag"] == '1'){
var txt="用户名或密码错误,请重新登录";
window.wxc.xcConfirm(txt);
}
}
});
注意的是jesult是json格式数据,如果result[“flag”]的值是true,if(true){}是不成立的
后台:
public void toLogin(){
JSONObject json = new JSONObject();
json.put("flag", "0");
this.renderText(json.toString()); //getHttpServletResponse().getWriter().write(text);
}
前后端交互:登录验证
本文探讨了使用jQuery的$.ajax方法实现前后端交互的登录验证过程,详细介绍了前台发送POST请求并处理JSON响应,以及后台如何构造JSON响应并返回。关键在于理解result['flag']的判断逻辑。
780

被折叠的 条评论
为什么被折叠?



