报错详细如下。
问题可能的原因
- 使用ajax提交时忘了阻止form的提交
- ajax会提交一次请求,但是在它提交之后,form又会再提交一次,导致ajax里面的事件出错,所以在js中click事件要结束时添加代码
return false;
不让form提交请求即可。
解决办法
修正后的源码如下。click事件要结束时添加return false;
<script type="text/javascript">
var $$ = mdui.JQ;
$(document).ready(function(){
//点击图片切换验证码
$("#vcodeImg").click(function(){
this.src="CpachaServlet?method=loginCapcha&t="+new Date().getTime();
});
//登录
$("#submitBtn").click(function(){
if($("#radio-2").attr("checked") && "${systemInfo.forbidStudent}" == 1){
mdui.snackbar({
message: '学生暂不能登录系统!',
position: 'left-top'
});
return false;
}
if($("#radio-3").attr("checked") && "${systemInfo.forbidTeacher}" == 1){
mdui.snackbar({
message: '教师暂不能登录系统!',
position: 'left-top'
});
return false;
}
var data = $("#form").serialize();
$.ajax({
type: "post",
url: "LoginServlet?method=Login",
data: data,
dataType: "text", //返回数据类型
success: function(msg){
if("vcodeError" == msg){
mdui.snackbar({
message: '验证码错误',
position: 'left-top',
});
$("#vcodeImg").click();//切换验证码
$("input[name='vcode']").val("");//清空验证码输入框
} else if("loginError" == msg){
mdui.snackbar({
message: '用户名或密码错误',
position: 'left-top',
});
$("#vcodeImg").click();//切换验证码
$("input[name='vcode']").val("");//清空验证码输入框
} else if("loginSuccess" == msg){
window.location.href = "SystemServlet?method=toAdminView";
} else{
alert(msg);
}
},
error: function(ex) {
alert(ex);
//请求出错处理
}
});
return false;
//在此处click事件结束时添加即可
});
});
</script>
版权属于:ic翼
本文链接:https://bingyishow.top/Technical-article/157.html
您必须遵守 署名-非商业性使用-相同方式共享 CC BY-NC-SA 使用这篇文章