修改后端登陆响应为json,修复登陆页面

This commit is contained in:
qing committed 2026-07-21 22:27:48 +08:00
1 parent 0811dd7960
commit 0399715008
2 files changed
+8 -6

No files matched your search

+5 -1
View File
@@ -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) {
+3 -5
View File
@@ -146,16 +146,14 @@ 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('登录成功,但服务器返回的数据不完整');
}
alert(`${result.message},账号:${result.data.username}`);
sessionStorage.setItem('token', result.data.token); sessionStorage.setItem('token', result.data.token);
location.href = 'main.html'; location.href = 'main.html';
} else { } else {
alert(result.message || '登录失败'); alert(result.message || '登录失败');
} }
} catch (err) { } catch (err) {
alert(err.message || '接口请求失败,请检查后端服务是否启动'); alert(err.message || '接口请求失败,请检查后端服务是否启动');
console.error('登录请求异常:', err); console.error('登录请求异常:', err);