dongwenghe2416 2019-03-05 18:24
浏览 99
已采纳

PHP json_encode()将Undefined返回到$ .getJSON

I'm creating an array in php and then using json_encode() before getting it with $.getJSON but it returns Undefined. If I send just one row to JS it working fine (json_encode($classes[0])) but when I try to send all the data it returns Undefined. Any ideas?

Here is a little of the array which is generated by querying the database and returning the results...

   Array
(
    [0] => Array
        (
            [Record_Type] => S
            [Convention_ID] => 
            [Event_ID] => 19030145
            [Sponsor_ID] => Google
            [Course_ID] => 2837199
            [Last_Modified_Date_Time] => DateTime Object
                (
                    [date] => 2019-02-25 10:03:36.000000
                    [timezone_type] => 3
                    [timezone] => US/Pacific
                )

        )

    [1] => Array
        (
            [Record_Type] => S
            [Convention_ID] => 
            [Event_ID] => 19030111
            [Sponsor_ID] => Google
            [Course_ID] => 2837192
            [Last_Modified_Date_Time] => DateTime Object
                (
                    [date] => 2019-02-21 07:23:47.000000
                    [timezone_type] => 3
                    [timezone] => US/Pacific
                )

        )

here is the php minus the SQL statement...

$stmt = sqlsrv_query($conn, $sql);

$classes = array();
while( $class = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
    $classes[] = $class;
}

echo $_GET['callback'] . '('.json_encode($classes).')';

and here is the Javascript which I'm using to get the JSON data...

$(function() {
    $.getJSON("https://example/api/education.php?callback=?",
        function(data) {        
            console.log(data);          
        }
    );
});
  • 写回答

1条回答 默认 最新

  • doutao5419 2019-03-05 22:34
    关注

    I found the problem. It wasn't in the code it was in the data returned from the SQL query. This character � was part of a string stored in the database. That one character somehow made the JSON return Undefined. When I removed � from the database everything started working fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效