Ext.JSON.decode(): You're trying to decode an invalid JSON String

无人久伴 提交于 2020-01-30 08:19:07

问题


I'm pretty new at this and have been trying to fix my problem since 2 weeks now hope you can help.

It seems my json output is not valid but I'm not sure if my problem is coming from my php or my extjs script.

I have a combo box wich should show me a list of choice when I click on it. The list is basically comming from a Sql table.

When I check in my console in Chrome I see my output and it seems fine.

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"}] 

I should see kappa and be able to select it but I have nothing just the json error.

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

    ?>

And heres my 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'
    }

});

回答1:


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

According to the Ext.JSON.decode method's source where the error is occurring, the response from PHP should be following :

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

This is not a valid JSON response. It should be :

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

According to the source provided, I suspect the only place from where "Connection to DB succeed" additional string should be coming is :

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

From headers.php remove echo statement "Connection to DB succeed".



来源:https://stackoverflow.com/questions/50244062/ext-json-decode-youre-trying-to-decode-an-invalid-json-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!