From 5a8a6db0e513967c842c0f88729865a480fba378 Mon Sep 17 00:00:00 2001 From: canglan Date: Thu, 2 Apr 2026 01:32:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=8C=E7=BB=B4=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=99=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/common.js | 17 +++++++++++++---- pages/qrcode.php | 7 ++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/js/common.js b/js/common.js index 9f84445..f9d3563 100644 --- a/js/common.js +++ b/js/common.js @@ -24,7 +24,6 @@ function isLoggedIn() { // ========== API 请求封装 ========== async function apiRequest(endpoint, options = {}) { - // 使用 window.API_BASE,如果未定义则使用默认值 const baseUrl = window.API_BASE || '/api/v1'; const url = endpoint.startsWith('http') ? endpoint : `${baseUrl}${endpoint}`; @@ -52,11 +51,21 @@ async function apiRequest(endpoint, options = {}) { return null; } - const data = await response.json(); + // 先获取响应文本,用于调试 + const text = await response.text(); + if (!response.ok) { - throw new Error(data.detail || data.message || '请求失败'); + let errorMsg; + try { + const errorData = JSON.parse(text); + errorMsg = errorData.detail || errorData.message || JSON.stringify(errorData); + } catch (e) { + errorMsg = text || '请求失败'; + } + throw new Error(errorMsg); } - return data; + + return JSON.parse(text); } catch (error) { console.error('API 请求错误:', error); throw error; diff --git a/pages/qrcode.php b/pages/qrcode.php index cee7cf1..2e164c5 100644 --- a/pages/qrcode.php +++ b/pages/qrcode.php @@ -47,19 +47,20 @@ async function generateQR() { return; } - const size = document.getElementById('size').value; + const size = parseInt(document.getElementById('size').value); try { const data = await apiRequest('/qrcode/generate', { method: 'POST', - body: JSON.stringify({ content, size: parseInt(size) }) + body: JSON.stringify({ content: content, size: size }) }); currentQRCode = data.qr_code; document.getElementById('qrContainer').innerHTML = `二维码`; document.getElementById('downloadBtn').classList.remove('hidden'); } catch (error) { - showToast(error.message, 'error'); + console.error('生成失败:', error); + showToast('生成失败: ' + (error.message || '未知错误'), 'error'); } }