Fix: 前端 Ajax 请求缺少 Accept 头导致 404 返回 HTML 引发 JSON.parse 异常
This commit is contained in:
@@ -93,7 +93,20 @@
|
|||||||
/** 获取用户资料 */
|
/** 获取用户资料 */
|
||||||
async fetchUser(username) {
|
async fetchUser(username) {
|
||||||
try {
|
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();
|
const data = await res.json();
|
||||||
if (data.status === 'success') {
|
if (data.status === 'success') {
|
||||||
this.userInfo = data.data;
|
this.userInfo = data.data;
|
||||||
@@ -103,7 +116,7 @@
|
|||||||
this.whisperList = [];
|
this.whisperList = [];
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error('Error fetching user:', e);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -333,7 +346,8 @@
|
|||||||
<div x-show="userInfo.last_ip !== undefined"
|
<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;">
|
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;">
|
<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 style="display: flex; flex-direction: column; gap: 3px;">
|
||||||
<div><span style="opacity: 0.8;">主要IP:</span><span x-text="userInfo.last_ip || '无'"></span>
|
<div><span style="opacity: 0.8;">主要IP:</span><span x-text="userInfo.last_ip || '无'"></span>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user