登陆
<body>
<h1>登录页面</h1>
<form id="form">
手机号:<input type="text" id="mobile" name="mobile"><br>
密 码:<input type="text" id="password" name="password"><br>
<button type="submit">登录</button>
</form>
//引入md5进行密码加密
<script src="./md5.js"></script>
<script>
var form = document.getElementById('form');
var mobile = document.getElementById('mobile');
var password = document.getElementById('password');
form.onsubmit = function () {
event.preventDefault();
// 验证手机号
var patt = /^1[3-9]\d{9}$/;
if (!patt.test(mobile.value)) return alert('请输入正确的手机号!');
// 验证密码
if (!password.value) return alert('请输入密码');
// 验证通过,发送请求
var data = new FormData(form);
data.set('password', hex_md5(password.value));
fetch('http://192.168.0.1:8080/api/login', {
method: 'post',
body: data
}).then(res => res.json()).then(res => {
console.log(res);
if (res.code === 1) {
// 将uuid存到本地储存里面
// localStorage在不同网站之间,储存的内容是不共享的
localStorage.setItem('uuid', res.uuid);
location.href = './index.html';
} else {
alert(res.message);
}
})
}
</script>
</body>
登陆
<body>
<h1>登录页面</h1>
<form id="form">
手机号:<input type="text" id="mobile" name="mobile"><br>
密 码:<input type="text" id="password" name="password"><br>
<button type="submit">登录</button>
</form>
//引入md5进行密码加密
<script src="./md5.js"></script>
<script>
var form = document.getElementById('form');
var mobile = document.getElementById('mobile');
var password = document.getElementById('password');
form.onsubmit = function () {
event.preventDefault();
// 验证手机号
var patt = /^1[3-9]\d{9}$/;
if (!patt.test(mobile.value)) return alert('请输入正确的手机号!');
// 验证密码
if (!password.value) return alert('请输入密码');
// 验证通过,发送请求
var data = new FormData(form);
data.set('password', hex_md5(password.value));
fetch('http://192.168.0.1:8080/api/login', {
method: 'post',
body: data
}).then(res => res.json()).then(res => {
console.log(res);
if (res.code === 1) {
// 将uuid存到本地储存里面
// localStorage在不同网站之间,储存的内容是不共享的
localStorage.setItem('uuid', res.uuid);
location.href = './index.html';
} else {
alert(res.message);
}
})
}
</script>
</body>