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.

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

报告相同问题?

悬赏问题

  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退