diff --git a/yuthon_ai_agent/static/src/js/ai_chat_action.js b/yuthon_ai_agent/static/src/js/ai_chat_action.js index 81e78b8e2..3fc860609 100644 --- a/yuthon_ai_agent/static/src/js/ai_chat_action.js +++ b/yuthon_ai_agent/static/src/js/ai_chat_action.js @@ -95,21 +95,23 @@ export class AiChatAction extends Component { body: formData, credentials: "same-origin", }); + // 先读 text,再尝试解析 JSON(避免 body stream already read) + const text = await resp.text(); + let data; + try { + data = JSON.parse(text); + } catch (_) { + data = null; + } + if (!resp.ok) { - let detail = ""; - try { - const errData = await resp.json(); - detail = errData.error || JSON.stringify(errData); - } catch (_) { - const text = await resp.text(); - detail = text.substring(0, 500); - } + let detail = (data && data.error) || text.substring(0, 300); console.error("[AI] Upload HTTP", resp.status, detail); - alert("上传失败 (HTTP " + resp.status + ")\n" + (detail || "未知错误")); + alert("上传失败 (HTTP " + resp.status + ")\n" + detail); return; } - const data = await resp.json(); - if (data.success) { + + if (data && data.success) { this.state.attachments = [ ...this.state.attachments, { @@ -119,7 +121,7 @@ export class AiChatAction extends Component { }, ]; } else { - alert(data.error || "上传失败"); + alert((data && data.error) || "上传失败"); } } catch (e) { console.error("[AI] Upload failed:", e); diff --git a/yuthon_ai_agent/static/src/js/ai_floating.js b/yuthon_ai_agent/static/src/js/ai_floating.js index bcc8d269e..d5ab1c833 100644 --- a/yuthon_ai_agent/static/src/js/ai_floating.js +++ b/yuthon_ai_agent/static/src/js/ai_floating.js @@ -95,22 +95,23 @@ async function uploadFile(file) { body: formData, credentials: "same-origin", }); + // 先读 text,再尝试解析 JSON(避免 body stream already read) + const text = await resp.text(); + let data; + try { + data = JSON.parse(text); + } catch (_) { + data = null; + } + if (!resp.ok) { - // 服务器返回错误状态码,尝试读取错误详情 - let detail = ""; - try { - const errData = await resp.json(); - detail = errData.error || JSON.stringify(errData); - } catch (_) { - const text = await resp.text(); - detail = text.substring(0, 500); - } + let detail = (data && data.error) || text.substring(0, 300); console.error("[AI] Upload HTTP", resp.status, detail); - alert("上传失败 (HTTP " + resp.status + ")\n" + (detail || "未知错误")); + alert("上传失败 (HTTP " + resp.status + ")\n" + detail); return; } - const data = await resp.json(); - if (data.success) { + + if (data && data.success) { pendingAttachments.push({ attachment_id: data.attachment_id, filename: data.filename, @@ -119,7 +120,7 @@ async function uploadFile(file) { }); renderAttachmentPreview(); } else { - alert(data.error || "上传失败"); + alert((data && data.error) || "上传失败"); } } catch (e) { console.error("[AI] Upload failed:", e);