From 540fe7b54307f35ca42cccdde34d9edb5a343357 Mon Sep 17 00:00:00 2001 From: nanhaoluo <3075912108@qq.com> Date: Mon, 26 Jan 2026 14:31:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20AI=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E9=93=BE=E6=8E=A5=E8=BF=94=E5=9B=9E=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复 AJAX 函数中缺少 api_id 参数检查导致的错误 - 统一错误消息格式,使用数组包装错误信息 - 修复 JavaScript 中错误消息解析逻辑 - 改进错误提示的可读性和调试信息 --- functions.php | 20 +++++++++++++------- settings.php | 6 +++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/functions.php b/functions.php index 453822b..94682df 100644 --- a/functions.php +++ b/functions.php @@ -8787,15 +8787,21 @@ function argon_ajax_test_unified_api() { check_ajax_referer('argon_test_unified_api', 'nonce'); if (!current_user_can('manage_options')) { - wp_send_json_error('权限不足'); + wp_send_json_error(['message' => '权限不足']); + return; + } + + $api_id = isset($_POST['api_id']) ? sanitize_text_field($_POST['api_id']) : ''; + + if (empty($api_id)) { + wp_send_json_error(['message' => '缺少 API ID 参数']); return; } - $api_id = sanitize_text_field($_POST['api_id']); $api = argon_get_api_by_id($api_id); if (!$api) { - wp_send_json_error('API 不存在'); + wp_send_json_error(['message' => 'API 不存在']); return; } @@ -8816,7 +8822,7 @@ function argon_ajax_test_unified_api() { $api_endpoint = !empty($api['api_endpoint']) ? $api['api_endpoint'] : (isset($default_endpoints[$api['provider']]) ? $default_endpoints[$api['provider']] : ''); if (empty($api_endpoint)) { - wp_send_json_error('未配置 API 端点'); + wp_send_json_error(['message' => '未配置 API 端点']); return; } @@ -8865,7 +8871,7 @@ function argon_ajax_test_unified_api() { $response_time = round((microtime(true) - $start_time) * 1000); if (is_wp_error($response)) { - wp_send_json_error('连接失败: ' . $response->get_error_message()); + wp_send_json_error(['message' => '连接失败: ' . $response->get_error_message()]); return; } @@ -8880,7 +8886,7 @@ function argon_ajax_test_unified_api() { 'response_time' => $response_time ]); } else { - wp_send_json_error('API 返回格式异常: ' . substr($body, 0, 200)); + wp_send_json_error(['message' => 'API 返回格式异常: ' . substr($body, 0, 200)]); } } else { $error_msg = 'HTTP ' . $status_code; @@ -8890,7 +8896,7 @@ function argon_ajax_test_unified_api() { } else { $error_msg .= ': ' . substr($body, 0, 200); } - wp_send_json_error($error_msg); + wp_send_json_error(['message' => $error_msg]); } } add_action('wp_ajax_argon_test_unified_api', 'argon_ajax_test_unified_api'); diff --git a/settings.php b/settings.php index 4d47fe9..4cfac80 100644 --- a/settings.php +++ b/settings.php @@ -2455,8 +2455,8 @@ function themeoptions_page(){ if (response.success) { alert('✓ ' + response.data.message); } else { - // wp_send_json_error 的错误消息在 response.data 中 - let errorMsg = response.data || ''; + // wp_send_json_error 的错误消息在 response.data.message 中 + let errorMsg = (response.data && response.data.message) ? response.data.message : ''; alert('✗ ' + errorMsg); } $btn.prop('disabled', false).html(originalHtml); @@ -2464,7 +2464,7 @@ function themeoptions_page(){ // AJAX 请求本身失败 console.error('AJAX Error:', xhr, status, error); console.error('Response Text:', xhr.responseText); - alert(''); + alert('✗ ' + (xhr.responseText || error || '')); $btn.prop('disabled', false).html(originalHtml); }); });