浏览代码

Ajax debugging

svalavuo 4 天之前
父节点
当前提交
3c23d6689a
共有 2 个文件被更改,包括 47 次插入20 次删除
  1. 21 5
      admin/ajax_import_test.php
  2. 26 15
      admin/ajax_test_interface.html

+ 21 - 5
admin/ajax_import_test.php

@@ -1,14 +1,28 @@
 <?php
 // AJAX-based WordPress import test to bypass form submission issues
-header('Content-Type: application/json');
+ob_start(); // Capture all output
+
 error_reporting(E_ALL);
-ini_set('display_errors', 1);
+ini_set('display_errors', 0); // Don't display errors, capture them instead
 ini_set('max_execution_time', 60);
 ini_set('memory_limit', '512M');
 
-require_once '../includes/config.php';
-require_once '../includes/database.php';
-require_once '../includes/wordpress_import.php';
+try {
+    require_once '../includes/config.php';
+    require_once '../includes/database.php';
+    require_once '../includes/wordpress_import.php';
+} catch (Exception $e) {
+    ob_clean();
+    header('Content-Type: application/json');
+    echo json_encode([
+        'success' => false, 
+        'message' => 'Include error: ' . $e->getMessage(),
+        'step' => 'includes'
+    ]);
+    exit;
+}
+
+header('Content-Type: application/json');
 
 $response = ['success' => false, 'message' => '', 'step' => ''];
 
@@ -106,5 +120,7 @@ try {
     ];
 }
 
+// Clean any output and send JSON
+ob_clean();
 echo json_encode($response);
 ?>

+ 26 - 15
admin/ajax_test_interface.html

@@ -77,23 +77,34 @@
             log(`Making request: ${action}`, 'info');
             
             return fetch(`ajax_import_test.php?${params}`)
-                .then(response => response.json())
-                .then(data => {
-                    if (data.success) {
-                        log(`✓ ${data.message} (${data.step})`, 'success');
-                        if (data.stats) {
-                            log(`Stats: ${JSON.stringify(data.stats)}`, 'info');
-                        }
-                        if (data.result) {
-                            log(`Result: ${JSON.stringify(data.result)}`, 'info');
-                        }
+                .then(response => {
+                    const contentType = response.headers.get('content-type');
+                    if (contentType && contentType.includes('application/json')) {
+                        return response.json().then(data => {
+                            if (data.success) {
+                                log(`✓ ${data.message} (${data.step})`, 'success');
+                                if (data.stats) {
+                                    log(`Stats: ${JSON.stringify(data.stats)}`, 'info');
+                                }
+                                if (data.result) {
+                                    log(`Result: ${JSON.stringify(data.result)}`, 'info');
+                                }
+                            } else {
+                                log(`✗ ${data.message} (${data.step})`, 'error');
+                                if (data.error) {
+                                    log(`Error details: ${data.error.file}:${data.error.line}`, 'error');
+                                }
+                            }
+                            return data;
+                        });
                     } else {
-                        log(`✗ ${data.message} (${data.step})`, 'error');
-                        if (data.error) {
-                            log(`Error details: ${data.error.file}:${data.error.line}`, 'error');
-                        }
+                        // Handle HTML response (likely PHP error)
+                        return response.text().then(html => {
+                            log(`⚠ Server returned HTML instead of JSON:`, 'error');
+                            log(`First 500 chars: ${html.substring(0, 500)}`, 'error');
+                            throw new Error('Server returned HTML instead of JSON');
+                        });
                     }
-                    return data;
                 })
                 .catch(error => {
                     log(`Network error: ${error.message}`, 'error');