接口使用说明:
点开“用户管理”下“用户登录”,“try it out”
输入接口参数 username:admin password:123456。点击execute
查看返回结果,token为当前用户的身份令牌
复制身份令牌,点击页面右上角的“Authorization”,粘贴后点击按钮“Authorize”
点击“Close”关闭弹窗。
测试其他接口,输入相关参数,点击“execute”即可正常运行。
实验内容:
1、设计登录界面,输入账号、密码(测试账号:admin 密码:123456)后调用服务端Api接口进行身份验证验证。登录成功记录所返回的身份令牌,登录失败给出相应反馈提示。
- 接口地址:http:// 114.67.241.121:8088/user/login
- 请求方式:post
- 接口参数
- 返回数据
登陆
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #F2F4F7;
}
form {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background-color: white;
border-radius: 24px;
width: 600px;
height: 350px;
}
.login {
position: relative;
left: 50%;
transform: translate(-50%);
width: 360px;
height: 48px;
font-size: 18px;
background-color: rgba(39, 125, 255, 0.3);
border-radius: 24px;
margin-top: 40px;
}
.capital {
width: 200px;
height: 30px;
font-size: 20px;
text-indent: 3em;
font-weight: bolder;
}
.box {
padding: 68px 90px 51px;
width: 540px;
}
#uname input{
width: 100%;
height: 48px;
border: 1px solid #d4d6d9;
font-size: 14px;
border-radius: 24px;
box-sizing: border-box;
padding: 0 24px 0 54px;
}
#pw input {
width: 100%;
height: 48px;
border: 1px solid #d4d6d9;
font-size: 14px;
border-radius: 24px;
box-sizing: border-box;
padding: 0 24px 0 54px;
}
div {
margin: 15px;
}
</style>
</head>
<body>
<form>
<div class="capital">用户登录</div>
<hr>
<div id="uname">
<input type="text" placeholder="请输入你的账号">
</div>
<div id="pw">
<input type="password" placeholder="请输入密码">
</div>
<input type="button" value="登陆" class="login" id="btn">
</form>
<script src="./jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#btn').on('click', function() {
let uname = $('#uname > input').val();
let pw = $('#pw > input').val();
$.post('http://114.67.241.121:8088/user/login', { username: uname, password: pw }, function (e) {
if (e.success) {
localStorage.setItem('token', e.data.token);
console.log(e);
location.href = './主界面.html';
} else {
alert('密码或账号错误。请重新输入');
}
})
})
})
</script>
</body>
</html>
主界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
position: absolute;
left: 50%;
top: 30%;
transform: translate(-50%, -50%);
}
input {
display: block;
margin: 15px;
width: 300px;
height: 35px;
border-radius: 24px;
font-size: 18px;
}
h2 {
text-indent: 115px;
}
</style>
</head>
<body>
<div>
<h2>功能界面</h2>
<hr>
<input type="button" name="" id="look" value="查看">
<input type="button" name="" id="add" value="增加">
<input type="button" name="" id="update" value="修改">
<input type="button" name="" id="delete" value="删除">
</div>
<script src="./jquery.min.js"></script>
<script>
$('#look').on('click', function() {
location.href = './显示学生信息.html';
})
$('#add').on('click', function () {
location.href = './添加.html';
})
$('#update').on('click', function () {
location.href = './修改.html';
})
$('#delete').on('click', function () {
location.href = './删除.html';
})
</script>
</body>
</html>
显示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
height: 500px;
}
table {
border-collapse: collapse;
text-align: center;
}
table tr th {
height: 30px;
}
table caption {
margin: 10px;
font-weight: bolder;
font-size: 24px;
}
</style>
</head>
<body>
<div>
<table border="1">
<caption>学生信息</caption>
<thead>
<tr>
<th>stuid</th>
<th>stuno</th>
<th>stuname</th>
<th>stumajor</th>
<th>stugender</th>
<th>stugrade</th>
<th>stuphone</th>
<th>stuaddess</th>
<th>stunative</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script src="./jquery.min.js"></script>
<script>
$(document).ready(function() {
let token = localStorage.getItem('token');
$.ajax({
type: 'GET',
url: 'http://114.67.241.121:8088/stu/list',
headers: {
'Authorization': token,
},
success: function(e) {
console.log(e);
$(e.data).each(function(index, stu) {
let temp = $(`
<tr>
<td id='stuid'>${stu.stuid}</td>
<td id='stuno'>${stu.stuno}</td>
<td id='stuname'>${stu.stuname}</td>
<td id='stumajor'>${stu.stumajor}</td>
<td id='stugender'>${stu.stugender}</td>
<td id='stugrade'>${stu.stugrade}</td>
<td id='stuphone'>${stu.stuphone}</td>
<td id='stuaddess'>${stu.stuaddess}</td>
<td id='stunative'>${stu.stunative}</td>
</tr>`);
temp.appendTo('tbody');
})
}
})
})
</script>
</body>
</html>
增加
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
position: absolute;
left: 50%;
transform: translate(-50%);
width: 330px;
}
.box div p {
margin: 0px;
}
.box div input {
margin: 0px;
width: 300px;
height: 20px;
border-radius: 24px;
padding: 0 10px 0;
}
.box div {
width: 200px;
height: 50px;
}
h1 {
text-align: center;
}
#add {
position: relative;
left: 35%;
margin-top: 10px;
width: 100px;
border-radius: 24px;
}
</style>
</head>
<body>
<div class="box">
<h1>增加学生</h1>
<div>
<p>stuid:</p>
<input type="text" id="stuid">
</div>
<div>
<p>stuno:</p>
<input type="text" id="stuno">
</div>
<div>
<p>stuname:</p>
<input type="text" id="stuname">
</div>
<div>
<p>stumajor:</p>
<input type="text" id="stumajor">
</div>
<div>
<p>stugender:</p>
<input type="text" id="stugender">
</div>
<div>
<p>stugrade:</p>
<input type="text" id="stugrade">
</div>
<div>
<p>stuphone:</p>
<input type="text" id="stuphone">
</div>
<div>
<p>stuaddess:</p>
<input type="text" id="stuaddess">
</div>
<div>
<p>stunative:</p>
<input type="text" id="stunative">
</div>
<input type="button" value="添加" id="add">
</div>
<script src="./jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#add').on('click', function() {
let token = localStorage.getItem('token');
let stu = {
stuid: $('#stuid').val(),
stuname: $('#stuname').val(),
stuno: $('#stuno').val(),
stugender: $('#stugender').val(),
stumajor: $('#stumajor').val(),
stugrade: $('#stugrade').val(),
stuphone: $('#stuphone').val(),
stuaddess: $('#stuaddess').val(),
stunative: $('#stunative').val(),
}
$.ajax({
type: 'POST',
url: 'http://114.67.241.121:8088/stu/add',
headers: {
'Authorization': token,
'Content-Type': 'application/json',
},
data: JSON.stringify(
stu,
),
success: function (e) {
console.log(e);
alert('添加成功');
location.href = './主界面.html';
}
})
})
})
</script>
</body>
</html>
修改
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
height: 500px;
}
table {
border-collapse: collapse;
text-align: center;
}
table tr th {
height: 30px;
text-align: center;
}
table caption {
margin: 10px;
font-weight: bolder;
font-size: 20px;
}
a {
text-decoration: none;
}
</style>
</head>
<body>
<div>
<table border="1">
<caption>学生信息</caption>
<thead>
<tr>
<th>stuid</th>
<th>stuno</th>
<th>stuname</th>
<th>stumajor</th>
<th>stugender</th>
<th>stugrade</th>
<th>stuphone</th>
<th>stuaddess</th>
<th>stunative</th>
<th>updata</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script src="./jquery.min.js"></script>
<script>
$(document).ready(function() {
let token = localStorage.getItem('token');
console.log(token);
$.ajax({
type: 'GET',
url: 'http://114.67.241.121:8088/stu/list',
headers: {
'Authorization': token,
},
success: function(e) {
$(e.data).each(function(index, stu) {
let temp = $(`
<tr>
<td id='stuid'>${stu.stuid}</td>
<td id='stuno'>${stu.stuno}</td>
<td id='stuname'>${stu.stuname}</td>
<td id='stumajor'>${stu.stumajor}</td>
<td id='stugender'>${stu.stugender}</td>
<td id='stugrade'>${stu.stugrade}</td>
<td id='stuphone'>${stu.stuphone}</td>
<td id='stuaddess'>${stu.stuaddess}</td>
<td id='stunative'>${stu.stunative}</td>
<td id="updata"><a href="#">修改</a></td>
</tr>`);
temp.appendTo('tbody');
})
let updatas = document.querySelectorAll('#updata');
console.log(updatas);
for (let i = 0; i < updatas.length; i ++) {
updatas[i].addEventListener('click', function () {
sessionStorage.setItem('stuJson', JSON.stringify(e.data[i]));
location.href = './修改界面.html';
})
}
}
})
})
</script>
</body>
</html>
修改界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
position: absolute;
left: 50%;
transform: translate(-50%);
width: 330px;
}
.box div p {
margin: 0px;
}
.box div input {
margin: 0px;
width: 300px;
height: 20px;
border-radius: 24px;
padding: 0 10px 0;
}
.box div {
width: 200px;
height: 50px;
}
h1 {
text-align: center;
}
#updata {
position: relative;
left: 35%;
margin-top: 10px;
width: 100px;
border-radius: 24px;
}
</style>
</head>
<body>
<div class="box">
<h1>修改学生</h1>
<div>
<p>stuid:</p>
<input type="text" id="stuid">
</div>
<div>
<p>stuno:</p>
<input type="text" id="stuno">
</div>
<div>
<p>stuname:</p>
<input type="text" id="stuname">
</div>
<div>
<p>stumajor:</p>
<input type="text" id="stumajor">
</div>
<div>
<p>stugender:</p>
<input type="text" id="stugender">
</div>
<div>
<p>stugrade:</p>
<input type="text" id="stugrade">
</div>
<div>
<p>stuphone:</p>
<input type="text" id="stuphone">
</div>
<div>
<p>stuaddess:</p>
<input type="text" id="stuaddess">
</div>
<div>
<p>stunative:</p>
<input type="text" id="stunative">
</div>
<input type="button" value="修改" id="updata">
</div>
<script src="./jquery.min.js"></script>
<script>
$(document).ready(function () {
var stu = JSON.parse(sessionStorage.getItem('stuJson'));
$(function () {
console.log(stu);
$('#stuid').val(stu.stuid);
$('#stuno').val(stu.stuno);
$('#stuname').val(stu.stuname);
$('#stugender').val(stu.stugender);
$('#stumajor').val(stu.stumajor);
$('#stuphone').val(stu.stuphone);
$('#stuaddess').val(stu.stuaddess);
$('#stunative').val(stu.stunative);
$('#stugrade').val(stu.stugrade);
});
$('#updata').on('click', function () {
let token = localStorage.getItem('token');
let stu = {
stuid: $('#stuid').val(),
stuname: $('#stuname').val(),
stuno: $('#stuno').val(),
stugender: $('#stugender').val(),
stumajor: $('#stumajor').val(),
stugrade: $('#stugrade').val(),
stuphone: $('#stuphone').val(),
stuaddess: $('#stuaddess').val(),
stunative: $('#stunative').val(),
}
$.ajax({
type: 'POST',
url: 'http://114.67.241.121:8088/stu/edit',
headers: {
'Authorization': token,
'Content-Type': 'application/json',
},
data: JSON.stringify(
stu,
),
success: function (e) {
console.log(e);
alert('修改成功');
location.href = './主界面.html';
}
})
})
})
</script>
</body>
</html>
删除
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
height: 500px;
}
table {
border-collapse: collapse;
text-align: center;
}
table tr th {
width: 100px;
height: 30px;
}
table caption {
margin: 10px;
font-size: 20px;
font-weight: bolder;
}
a {
text-decoration: none;
}
</style>
</head>
<body>
<div>
<table border="1">
<caption>学生信息</caption>
<thead>
<tr>
<th>stuid</th>
<th>stuno</th>
<th>stuname</th>
<th>stumajor</th>
<th>stugender</th>
<th>stugrade</th>
<th>stuphone</th>
<th>stuaddess</th>
<th>stunative</th>
<th>delete</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script src="./jquery.min.js"></script>
<script>
$(document).ready(function () {
let token = localStorage.getItem('token');
$.ajax({
type: 'GET',
url: 'http://114.67.241.121:8088/stu/list',
headers: {
'Authorization': token,
},
success: function (e) {
$(e.data).each(function (index, stu) {
let temp = $(`
<tr>
<td id='stuid'>${stu.stuid}</td>
<td id='stuno'>${stu.stuno}</td>
<td id='stuname'>${stu.stuname}</td>
<td id='stumajor'>${stu.stumajor}</td>
<td id='stugender'>${stu.stugender}</td>
<td id='stugrade'>${stu.stugrade}</td>
<td id='stuphone'>${stu.stuphone}</td>
<td id='stuaddess'>${stu.stuaddess}</td>
<td id='stunative'>${stu.stunative}</td>
<td id="delete"><a href="#">删除</a></td>
</tr>`);
temp.appendTo('tbody');
})
$('#delete').on('click', function() {
let id = $(this).parent().parent().find('#stuid').text();
$.ajax({
type: "POST",
url: 'http://114.67.241.121:8088/stu/del',
headers: {
'Authorization': token,
},
data: { 'stuid': id },
success: function (e) {
console.log(e);
$('#delete').parent().remove();
alert('删除成功');
location.href = './主界面.html';
}
});
})
}
})
})
</script>
</body>
</html>