You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ext.JSON解码无效JSON字符串求助:下拉框无法加载数据

Fixing Your ExtJS Dropdown Data Loading Issue

Hey there! Let's break down what's causing your dropdown to fail and get it working properly.

The Root Problem: Invalid JSON Response

That Chrome console error tells us everything: Ext.JSON.decode(): You're trying to decode an invalid JSON String. Looking at your returned content:

Connection to DB succeed [{"id":"3","businessunit":"kappa"}]

The extra text Connection to DB succeed is ruining the JSON structure. ExtJS expects a pure JSON string, but that prefix makes the entire response unparseable.


Step 1: Fix Your PHP Code to Return Clean JSON

First, we need to eliminate any extra output and ensure valid JSON formatting:

1. Remove the Debug/Status Output

Track down where Connection to DB succeed is being printed — it's likely in your headers.php file (when the DB connection succeeds) or a debug log statement. Delete that line entirely. API endpoints should only return JSON, no extra text, spaces, or debug messages.

2. Fix the Invalid JSON in Empty Data Scenarios

Your current else clause outputs (success:true,"error":0) — this is not valid JSON. We need to use curly braces and quote all keys properly:

echo '{"success":true,"error":0}';

Revised PHP Code

Here's a cleaned-up version of your PHP script with these fixes:

<?php 
require_once "../../_includes/headers.php"; 
$query = "select id, businessunit from Team_tab order by businessunit"; 
logit($query); 
$result = odbc_exec($connection,$query); 

$myArray = []; // Initialize array to avoid isset checks
while($row = odbc_fetch_array($result)) { 
    $myArray[] = [
        'id' => $row['id'],
        'businessunit' => $row['businessunit'],
    ]; 
} 

if (sizeof($myArray) > 0) { 
    echo json_encode($myArray); 
} else { 
    echo '{"success":true,"error":0}'; 
} 
?>

Step 2: Verify Your ExtJS Store Configuration

Your ComboBox is bound to the {businessunit} store, so make sure your ViewModel has the correct store setup. Here's what that should look like:

Ext.define('EmpMen.view.Employee.CreateEmployeeViewModel', {
    extend: 'Ext.app.ViewModel',
    alias: 'viewmodel.employee.createemployee',
    
    stores: {
        businessunit: {
            autoLoad: true, // Auto-load data when the store is created
            fields: ['id', 'businessunit'],
            proxy: {
                type: 'ajax',
                url: '/path/to/your/php/script.php', // Replace with your actual PHP file path
                reader: {
                    type: 'json',
                    rootProperty: '.' // Since PHP returns a raw array, no root key needed
                }
            }
        }
    }
});

How to Test

  1. Test the PHP Endpoint Directly: Paste your PHP URL into a browser. You should see only valid JSON, like:
    [{"id":"3","businessunit":"kappa"}]
    
    Or if there's no data:
    {"success":true,"error":0}
    
  2. Clear Cache & Retest: Refresh your ExtJS app with browser cache cleared (Ctrl+Shift+R on Chrome) — your dropdown should now load the data correctly.

内容的提问来源于stack exchange,提问作者Joel Deslauriers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:27:53