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'); } }