問題描述
我對此很陌生,并且自 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 字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!