【问题标题】:Ext.JSON.decode(): You're trying to decode an invalid JSON StringExt.JSON.decode(): 你试图解码一个无效的 JSON 字符串
【发布时间】:2018-05-09 00:55:21
【问题描述】:

我对此很陌生,自 2 周以来一直在尝试解决我的问题,希望您能提供帮助。

似乎我的 json 输出无效,但我不确定我的问题是来自我的 php 还是我的 extjs 脚本。

我有一个组合框,当我点击它时应该会显示一个选择列表。 该列表基本上来自 Sql 表。

当我在 Chrome 中签入我的控制台时,我看到了我的输出,而且看起来还不错。

ext-all-rtl-debug.js?_dc=1525825768241:10025 [E] Ext.JSON.decode(): You're trying to decode an invalid JSON String: Connection to DB succeed    
        [{"id":"3","businessunit":"kappa"}] 

我应该看到 kappa 并且能够选择它,但我没有任何内容,只有 json 错误。

这是我的 php:

    <?php
    require_once"..//..//_includes/headers.php";


$query = "select id, businessunit from Team_tab order by businessunit";
logit($query);
$result = odbc_exec($connection,$query);
while($row = odbc_fetch_array($result))
    {
    $myArray[] = array(
        'id'=>$row['id'],
        'businessunit'=>$row['businessunit'],
        );


    }

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

    ?>

这是我的 extjs:

Ext.define('EmpMen.view.Employee.CreateEmployee', {
        extend: 'Ext.window.Window',
        alias: 'widget.employee.createemployee',

    requires: [
        'EmpMen.view.Employee.CreateEmployeeViewModel',
        'EmpMen.view.Employee.CreateEmployeeViewController',
        'Ext.form.Panel',
        'Ext.form.field.ComboBox',
        'Ext.button.Button'
    ],

    controller: 'employee.createemployee',
    viewModel: {
        type: 'employee.createemployee'
    },
    reference: '',
    height: 325,
    itemId: 'createEmployee',
    width: 364,
    title: 'Enter Employee Information',

    layout: {
        type: 'vbox',
        align: 'stretch'
    },
    items: [
        {
            xtype: 'form',
            flex: 1,
            height: 170,
            padding: 10,
            width: 360,
            bodyPadding: 10,
            title: '',
            items: [
                {
                    xtype: 'textfield',
                    anchor: '100%',
                    reference: 'first',
                    itemId: 'first',
                    fieldLabel: 'First Name'
                },
                {
                    xtype: 'textfield',
                    anchor: '100%',
                    reference: 'last',
                    itemId: 'last',
                    fieldLabel: 'Last Name'
                },
                {
                    xtype: 'textfield',
                    anchor: '100%',
                    reference: 'tle',
                    itemId: 'tle',
                    fieldLabel: 'Title'
                },
                {
                    xtype: 'combobox',
                    anchor: '100%',
                    reference: 'bunit',
                    fieldLabel: 'Business Unit',
                    displayField: 'businessunit',
                    valueField: 'id',
                    bind: {
                        store: '{businessunit}'
                    }
                },
                {
                    xtype: 'textfield',
                    anchor: '100%',
                    reference: 'exp',
                    itemId: 'exp',
                    fieldLabel: 'Experience'
                },
                {
                    xtype: 'container',
                    margin: 10,
                    layout: {
                        type: 'hbox',
                        align: 'stretch',
                        pack: 'center'
                    },
                    items: [
                        {
                            xtype: 'button',
                            flex: 1,
                            reference: 'employeeForm',
                            maxWidth: 100,
                            width: '',
                            text: 'Save',
                            listeners: {
                                click: 'onButtonClick'
                            }
                        }
                    ]
                }
            ]
        }
    ],
    listeners: {
        afterrender: 'onCreateEmployeeAfterRender'
    }

});

【问题讨论】:

    标签: php json extjs


    【解决方案1】:

    来源:https://docs.sencha.com/extjs/6.0.2/modern/src/JSON.js.html#Ext.JSON-method-decode

    根据发生错误的Ext.JSON.decode方法的来源,PHP的响应应该如下:

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

    这不是有效的 JSON 响应。应该是:

    [{"id":"3","businessunit":"kappa"}]
    

    根据提供的来源,我怀疑“连接到数据库成功”附加字符串的唯一位置应该是:

    require_once"..//..//_includes/headers.php";
    

    从 headers.php 中删除 echo 语句“连接到数据库成功”。

    【讨论】:

    • 它可以在我的 db_connection.php 中删除 Echo。因此,据我所知,json decode 试图解码与 DB 的连接成功。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多