沃梦达 / 编程问答 / php问题 / 正文

Ext.JSON.decode():您正在尝试解码无效的 JSON 字符串

Ext.JSON.decode(): You#39;re trying to decode an invalid JSON String(Ext.JSON.decode():您正在尝试解码无效的 JSON 字符串)

本文介绍了Ext.JSON.decode():您正在尝试解码无效的 JSON 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此很陌生,并且自 2 周以来一直在努力解决我的问题,希望您能提供帮助.

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

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

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.

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

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.

当我在 Chrome 中检查我的控制台时,我看到了我的输出,看起来不错.

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

我应该看到 kappa 并且能够选择它,但我没有任何东西,只是 json 错误.

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

这是我的 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:

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'
    }

});

推荐答案

来源: https://docs.sencha.com/extjs/6.0.2/modern/src/JSON.js.html#Ext.JSON 方法解码

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

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

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

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";

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

这篇关于Ext.JSON.decode():您正在尝试解码无效的 JSON 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Ext.JSON.decode():您正在尝试解码无效的 JSON 字符串