Files
nxxmdata/bank-frontend/test-api.html

82 lines
3.1 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>