【问题标题】:You're trying to decode an invalid JSON String Error您正在尝试解码无效的 JSON 字符串错误
【发布时间】:2018-05-18 14:07:32
【问题描述】:

我正在尝试使用PHPSQL 源输出Grid,但它不起作用。

这是我在控制台中遇到的错误:

[W] For WAI-ARIA compliance, IMG elements SHOULD have an alt attribute.
[E] Ext.JSON.decode(): You're trying to decode an invalid JSON String: 
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: id in C:\wamp64\www\EmployeeMenu\_data\employee\Read_employee.php on line <i>14</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0019</td><td bgcolor='#eeeeec' align='right'>242280</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp64\www\EmployeeMenu\_data\employee\Read_employee.php' bgcolor='#eeeeec'>...\Read_employee.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: id in C:\wamp64\www\EmployeeMenu\_data\employee\Read_employee.php on line <i>14</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0019</td><td bgcolor='#eeeeec' align='right'>242280</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp64\www\EmployeeMenu\_data\employee\Read_employee.php' bgcolor='#eeeeec'>...\Read_employee.php<b>:</b>0</td></tr>
</table></font>
[{"id":null,"firstname":"Alain","lastname":"Doyon","title":"Project Manager","businessunit":"Dev","experience":"7"},{"id":null,"firstname":"Joel","lastname":"Deslauriers","title":"Integrator Senior","businessunit":"Kappa","experience":"5"}]

这是我的 PHP 文件:

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



$query = "select firstname, lastname, title, businessunit, experience from 
employee_tab order by businessunit";
logit($query);
$result = odbc_exec($connection,$query);
$cnt = 0;
while($row = odbc_fetch_array($result))

{ 
$cnt = $cnt + 1;
$myArray[] = array(
    'id'=>$row['id'],
    'firstname'=>$row['firstname'],
    'lastname'=>$row['lastname'],
    'title'=>$row['title'],
    'businessunit'=>$row['businessunit'],
    'experience'=>$row['experience'],
    );


}

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

?>

感谢您的帮助。

【问题讨论】:

  • 结果集中有'id'吗?尝试不使用 'id'=>$row['id']。
  • 你的 PHP 输出以一个通知开头,它永远不是一个有效的 Json。您应该遵循通知并“清理”您的 php 代码。
  • 此外,您仅在“echo $output”时回显有效的json,否则您的回显不会输出有效的json。
  • 您好,请删除通知和 tje rowid,因为我的这部分代码没有使用 ID 列,它工作正常。感谢您的帮助。

标签: php sql-server extjs sencha-architect


【解决方案1】:

你得到这个是因为你打开了display_errors,因此得到了关于事情的警告,破坏了你的 JSON。

在你的 php.ini 中关闭它

最好的错误记录体验是在终端中跟踪日志而不显示在屏幕上。

首先检查第 14 行的 Read_employee.php isset()!祝你好运

【讨论】:

  • 或在php文件ini_set('display_errors', 0); ini_set('display_startup_errors', 0);禁用它
【解决方案2】:

消息是自我解释的:Notice: Undefined index: id in C:\wamp64\www\EmployeeMenu\_data\employee\Read_employee.php on line &lt;i&gt;14&lt;/i&gt;

您的 $query 请求将仅返回 "firstname, lastname, title, businessunit, experience" 列,而不是 "id" ,但您在 while 循环中使用 ['id'] 。等paf,警告!

【讨论】:

  • 真正的问题是他在屏幕上显示错误,破坏了他的输出 json。是的,他应该解决这个问题,但他需要将其关闭,然后检查他的日志!
猜你喜欢
  • 2017-02-02
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多