添加银行后端接口,前端代码
This commit is contained in:
81
bank-frontend/test-api.html
Normal file
81
bank-frontend/test-api.html
Normal file
@@ -0,0 +1,81 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>API测试</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>API测试页面</h1>
|
||||
<button onclick="testLogin()">测试登录</button>
|
||||
<button onclick="testProjects()">测试项目接口</button>
|
||||
<div id="result"></div>
|
||||
|
||||
<script>
|
||||
let token = '';
|
||||
|
||||
async function testLogin() {
|
||||
try {
|
||||
const response = await fetch('http://localhost:5351/api/auth/login', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username: 'admin',
|
||||
password: 'Admin123456'
|
||||
})
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
console.log('登录响应:', data);
|
||||
|
||||
if (data.success) {
|
||||
token = data.data.token;
|
||||
document.getElementById('result').innerHTML =
|
||||
'<p style="color: green;">登录成功!Token: ' + token.substring(0, 20) + '...</p>';
|
||||
} else {
|
||||
document.getElementById('result').innerHTML =
|
||||
'<p style="color: red;">登录失败: ' + data.message + '</p>';
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('登录错误:', error);
|
||||
document.getElementById('result').innerHTML =
|
||||
'<p style="color: red;">登录错误: ' + error.message + '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
async function testProjects() {
|
||||
if (!token) {
|
||||
document.getElementById('result').innerHTML =
|
||||
'<p style="color: red;">请先登录!</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('http://localhost:5351/api/projects?page=1&limit=12&search=&status=', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + token,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
console.log('项目接口响应:', data);
|
||||
|
||||
if (data.success) {
|
||||
document.getElementById('result').innerHTML =
|
||||
'<p style="color: green;">项目接口成功!项目数量: ' + data.data.projects.length + '</p>' +
|
||||
'<pre>' + JSON.stringify(data.data.projects.slice(0, 2), null, 2) + '</pre>';
|
||||
} else {
|
||||
document.getElementById('result').innerHTML =
|
||||
'<p style="color: red;">项目接口失败: ' + data.message + '</p>';
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('项目接口错误:', error);
|
||||
document.getElementById('result').innerHTML =
|
||||
'<p style="color: red;">项目接口错误: ' + error.message + '</p>';
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user