<form action="StudentList.html" method="post">
<p>用户名:<input id="uname" /></p>
<p>密 码:<input id="pwd" type="password" /></p>
<input type="submit" value="登录" onclick="return check()" />
</form>
<script type="text/javascript">
function checkUser() {
var uname = document.getElementById("uname")
if(uname.value.trim() == "") {
alert("用户名不能为空")
return false;
}
return true;
}
function checkPwd() {
var pwd = document.getElementById("pwd")
if(pwd.value.trim() == "") {
alert("密码不能为空")
return false;
}
return true;
}
function login() {
var uname = document.getElementById("uname").value; //获取id名为uname的value值
var pwd = document.getElementById("pwd").value; //获取id名为pwd的value值
var obj = $.ajax({
url: Config.routeUrl+"api/my/UserLogin", //接口路径
type: "post",
data: {
"user": uname,
"upwd": pwd
},
async: true, //默认为true表示异步请求,false表示同步请求
success: function(msg) {
if(msg) {
location.href = "StudentList.html";
sessionStorage.name = uname; //用本地存储保存用户名
return true;
} else {
alert("用户名或者密码错误!");
return false;
}
},
error: function(a, e) {
alert("系统错误!请联系管理员!" + a + "\n" + e);
}
});
return false;
}
function check() {
if(checkUser() && checkPwd() && login()) {
return true;
}
return false;
}
</script>