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 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码
  • ¥15 esp32烧录失败,具体情况在图片上
  • ¥15 selenium安装报错