Fix: 前端 Ajax 请求缺少 Accept 头导致 404 返回 HTML 引发 JSON.parse 异常

This commit is contained in:
2026-02-27 11:43:30 +08:00
parent 731792fab6
commit 5504243bbb

View File

@@ -93,7 +93,20 @@
/** 获取用户资料 */
async fetchUser(username) {
try {
const res = await fetch('/user/' + encodeURIComponent(username));
const res = await fetch('/user/' + encodeURIComponent(username), {
headers: {
'Accept': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
}
});
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
console.error('Failed to fetch user:', errorData.message || res.statusText);
// 如果是 404 或者 500 等错误,直接静默退出或提示
return;
}
const data = await res.json();
if (data.status === 'success') {
this.userInfo = data.data;
@@ -103,7 +116,7 @@
this.whisperList = [];
}
} catch (e) {
console.error(e);
console.error('Error fetching user:', e);
}
},
@@ -333,7 +346,8 @@
<div x-show="userInfo.last_ip !== undefined"
style="margin-top: 8px; padding: 8px 10px; background: #fee2e2; border: 1px dashed #fca5a5; border-radius: 8px; font-size: 11px; color: #991b1b;">
<div style="font-weight: bold; margin-bottom: 4px; display: flex; align-items: center; gap: 4px;">
<span>🛡️</span> 管理员视野</div>
<span>🛡️</span> 管理员视野
</div>
<div style="display: flex; flex-direction: column; gap: 3px;">
<div><span style="opacity: 0.8;">主要IP</span><span x-text="userInfo.last_ip || '无'"></span>
</div>