修复未登录时 API 返回 HTML 导致期权链 JSON 解析失败。

API 统一返回 401 JSON,并改进前端非 JSON 错误提示。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-01 11:51:13 +08:00
parent ed9fd05a84
commit e7a4934068
3 changed files with 25 additions and 2 deletions
+9
View File
@@ -5157,6 +5157,15 @@ def login_required(f):
def decorated(*args, **kwargs):
if AUTH_DISABLED or bool(session.get("logged_in")):
return f(*args, **kwargs)
# API 必须返回 JSON,否则前端 r.json() 会报 Unexpected token...
path = request.path or ""
wants_json = (
path.startswith("/api/")
or (request.headers.get("X-Requested-With") or "").lower() == "xmlhttprequest"
or "application/json" in (request.headers.get("Accept") or "")
)
if wants_json:
return jsonify({"ok": False, "msg": "未登录或会话过期,请重新登录"}), 401
return redirect("/login")
return decorated