Ajax SpringMvc 實現登錄請求


<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>登錄</title>

</head>
<body>
<div class="content">
<div class="headpic">
<img src="${pageContext.request.contextPath}/assets/images/template1/body.png" />
</div>
<form>
<div class="form-group">
<input type="text" class="form-control" placeholder="用戶名" id="account">
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="密碼" id="password">
</div>
<div type="submit" class="btn btn-info" onclick="login()">登錄</div>
<br/>
<br/>
<p id="message"></p>
</form>
</div>
</body>
<script type="text/javascript">
function login(){
var account = $("#account").val();
var pwd =$("#password").val();
var prts = document.getElementById("message");
if(account==""||account==null){
prts.style.color="red";
prts.innerHTML = "賬號不能為空";
return;
}
if(pwd==""||pwd==null){
prts.style.color="red";
prts.innerHTML = "密碼不能為空";
return;
}

$.ajax({
type:"get",
url:"${pageContext.request.contextPath}/platform/account/login?account="+account+"&password="+pwd,
dataType:"json",
error:function(data){
alert("請求失敗,網絡異常")
console.log(data);
},
success:function(data){
var code = data.status;
if(code == 200){
prts.style.color="green";
prts.innerHTML = "登錄成功";
window.location.href = "${pageContext.request.contextPath}/platform/home.jsp";
}else{
prts.style.color="red";
prts.innerHTML = "用戶名或者密碼錯誤";
}
}
});
}
</script>
</html>


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com