!1 merge wei into main
对之前页面的修复 Created-by: qingss0 Commit-by: qing Merged-by: qingss0 Description: 引入validation.js,修改Content-Type 注册修复 dos2unix 修改后端登陆响应为json,修复登陆页面 See merge request: qingss0/micro_servers!1
This commit is contained in:
4 files changed
+28
-27
No files matched your search
@@ -359,9 +359,13 @@ http::response<http::string_body> handleLogin(
|
|||||||
std::string token = Base64::base64_encode(data);
|
std::string token = Base64::base64_encode(data);
|
||||||
std::string sign = _PRIV.Sign(data);
|
std::string sign = _PRIV.Sign(data);
|
||||||
|
|
||||||
|
json::value jv_rr = {
|
||||||
|
{"success", true},
|
||||||
|
{"data", json::object{{"token", token + "_" + sign}}} // 双大括号
|
||||||
|
};
|
||||||
res.set("Connection", "close");
|
res.set("Connection", "close");
|
||||||
res.result(http::status::ok);
|
res.result(http::status::ok);
|
||||||
res.body() = token + "_" + sign;
|
res.body() = json::serialize(jv_rr);
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (const HttpException& e) {
|
catch (const HttpException& e) {
|
||||||
|
|||||||
+19
-21
@@ -117,20 +117,20 @@ form.addEventListener('submit', async function(e) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const jsonBody = JSON.stringify({
|
const jsonBody = JSON.stringify({
|
||||||
username: userOk,
|
username: userOk,
|
||||||
password: pwdOk
|
password: pwdOk
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
loginButton.disabled = true;
|
loginButton.disabled = true;
|
||||||
const res = await fetch('/login', {
|
const res = await fetch('/login', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded'
|
'Content-Type': 'application/json' // 告诉服务器内容为json
|
||||||
},
|
},
|
||||||
body: jsonBody
|
body: jsonBody
|
||||||
});
|
});
|
||||||
|
|
||||||
const responseText = await res.text();
|
const responseText = await res.text();
|
||||||
let result;
|
let result;
|
||||||
@@ -146,19 +146,17 @@ form.addEventListener('submit', async function(e) {
|
|||||||
throw new Error(result.message || `登录失败(HTTP ${res.status})`);
|
throw new Error(result.message || `登录失败(HTTP ${res.status})`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.code === 0) {
|
if (result.success) {
|
||||||
if (!result.data || !result.data.token || !result.data.username) {
|
alert(`登陆成功`);
|
||||||
throw new Error('登录成功,但服务器返回的数据不完整');
|
sessionStorage.setItem('token', result.data.token);
|
||||||
}
|
location.href = 'main.html';
|
||||||
alert(`${result.message},账号:${result.data.username}`);
|
} else {
|
||||||
sessionStorage.setItem('token', result.data.token);
|
|
||||||
location.href = 'main.html';
|
|
||||||
} else {
|
|
||||||
alert(result.message || '登录失败');
|
alert(result.message || '登录失败');
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
|
} catch (err) {
|
||||||
alert(err.message || '接口请求失败,请检查后端服务是否启动');
|
alert(err.message || '接口请求失败,请检查后端服务是否启动');
|
||||||
console.error('登录请求异常:', err);
|
console.error('登录请求异常:', err);
|
||||||
} finally {
|
} finally {
|
||||||
loginButton.disabled = false;
|
loginButton.disabled = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -183,11 +183,8 @@ form.addEventListener('submit', async function (e) {
|
|||||||
throw new Error(result.message || `注册失败(HTTP ${res.status})`);
|
throw new Error(result.message || `注册失败(HTTP ${res.status})`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.code === 0) {
|
if (result.success) {
|
||||||
if (!result.data || !result.data.username) {
|
alert('注册成功');
|
||||||
throw new Error('注册成功,但服务器返回的数据不完整');
|
|
||||||
}
|
|
||||||
alert(`${result.message},账号:${result.data.username}`);
|
|
||||||
form.reset();
|
form.reset();
|
||||||
sendCode = '';
|
sendCode = '';
|
||||||
codeTarget = '';
|
codeTarget = '';
|
||||||
@@ -196,6 +193,7 @@ form.addEventListener('submit', async function (e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
alert(result.message || '注册失败');
|
alert(result.message || '注册失败');
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('注册请求异常:', err);
|
console.error('注册请求异常:', err);
|
||||||
alert(err.message || '接口请求失败,请检查后端服务是否启动');
|
alert(err.message || '接口请求失败,请检查后端服务是否启动');
|
||||||
|
|||||||
@@ -42,6 +42,7 @@
|
|||||||
<span id="agreeTip"></span>
|
<span id="agreeTip"></span>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
<script src="js/validation.js"></script>
|
||||||
<script src="js/login.js"></script>
|
<script src="js/login.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in new issue
Block a user