From 83d907f2bf80b103a4465caf1dde551ffd45d12d Mon Sep 17 00:00:00 2001 From: txai-dev Date: Mon, 14 Sep 2026 13:39:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20apiPost/apiPut/apiDelete=20=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E6=A3=80=E6=9F=A5HTTP=E7=8A=B6=E6=80=81=E7=A0=81?= =?UTF-8?q?=E2=80=94=E2=80=94409=E9=A3=8E=E6=8E=A7=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E8=A2=AB=E8=AF=AF=E6=8A=A5=E4=B8=BA=E6=88=90?= =?UTF-8?q?=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/src/api.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/admin/src/api.js b/admin/src/api.js index 8467ef6..6897e65 100644 --- a/admin/src/api.js +++ b/admin/src/api.js @@ -10,12 +10,22 @@ export function apiGet(path) { .catch(e => { throw new Error(e.message) }) } +// HTTP 非 2xx 时抛出服务端返回的错误信息(否则 409/500 被当正常 JSON 处理) +function handleResponse(r) { + return r.json().then(j => { + if (!r.ok) { + throw new Error(j.error || j.message || `请求失败(${r.status})`) + } + return j + }) +} + export function apiPost(path, body) { return fetch(path, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${getToken()}` }, body: JSON.stringify(body) - }).then(r => r.json()) + }).then(handleResponse) } export function apiPut(path, body) { @@ -23,12 +33,12 @@ export function apiPut(path, body) { method: 'PUT', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${getToken()}` }, body: JSON.stringify(body) - }).then(r => r.json()) + }).then(handleResponse) } export function apiDelete(path) { return fetch(path, { method: 'DELETE', headers: { Authorization: `Bearer ${getToken()}` } - }).then(r => r.json()) + }).then(handleResponse) }