修复前端
This commit is contained in:
@@ -294,15 +294,34 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function parseJsonResponse(r) {
|
||||
const text = await r.text();
|
||||
if (!text) return {};
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch (e) {
|
||||
const snippet = text.slice(0, 200);
|
||||
throw new Error(
|
||||
`HTTP ${r.status} 响应不是 JSON:${snippet}${text.length > 200 ? "…" : ""}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function submitForm(path, formEl) {
|
||||
const id = document.getElementById("trade-account").value;
|
||||
const fd = new FormData(formEl);
|
||||
try {
|
||||
const r = await fetch(path + encodeURIComponent(id), { method: "POST", body: fd });
|
||||
const j = await r.json();
|
||||
const j = await parseJsonResponse(r);
|
||||
const res = j.result || {};
|
||||
const msgs = (res.messages || []).join("\n") || JSON.stringify(res, null, 2);
|
||||
showToast(msgs, !res.ok);
|
||||
const msgs =
|
||||
(res.messages || []).join("\n") ||
|
||||
res.error ||
|
||||
res.msg ||
|
||||
res.text ||
|
||||
JSON.stringify(res, null, 2);
|
||||
const failed = res.ok === false || r.status >= 400 || !!res.error || !!res.text;
|
||||
showToast(msgs || (failed ? "操作失败" : "已提交"), failed);
|
||||
if (res.ok && res.preview) {
|
||||
showTrendPreview(res);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user