javascript - 从 JSON 编码获取控制台中 undefined object

标签 javascript php jquery html json

我收到了 JSON 文件中某些对象的未定义消息,即使它们不为空。可以解析并正常工作的对象包括 model_no、likes、dislikes、average_min、average_max 和average_users。但其他对象(如 user_id、ulike_type、uaverage_min 等)在 Chrome 控制台中显示为未定义,即使它们在文件中具有值。谁能告诉我 JSON 编码有什么问题吗?

这是我的 ajax 代码:

   var number = $('input').map(function(){
         return $(this).attr('rel')
   }).get();

   $.ajax({
     url: "file.php",
     type: "POST",
     dataType:"JSON",
     data:{'group[]':number},
     success: function (data) {

          var item_html = "",
          $.each(data, function(i,item){
            console.log(item.dislikes);  // this one shows value
            console.log(item.users_id);  // undefined
            console.log(item.min) // undefined
            console.log(item.max) // undefined
         })


     }
});

PHP 文件:

<?php

if($_POST['group'])
{
  header("Content-Type: application/json");
  $callback = $_GET['callback'];

  require_once 'config.php';

  try {

    $dbh = new PDO("mysql:host=$hostname;dbname=$databasename", $username, $password);    
    $id_group = $_POST["group"];
    $in  = str_repeat('?,', count($id_group) - 1) . '?';    

$sql = "

SELECT * FROM 
(
   SELECT COUNT( c.app_id ) AS users_no, ROUND( AVG( c.min ) , 1 ) AS avg_min, ROUND( AVG( c.max ) , 1 ) AS avg_max, a.mid, a.likes, a.dislikes, b.model_no
        FROM  `likes_total` a
        RIGHT JOIN  `product_info` b ON a.mid = b.mid
        LEFT JOIN  `resale` c ON c.mid = b.mid
        WHERE b.model_no IN ($in)
        GROUP BY b.model_no
)TAB1
JOIN
(
   SELECT a.app_id,b.model_no,c.isbooked,d.min,d.max,e.like_type
     FROM `users_info` a
     JOIN `product_info` b
       ON b.model_no IN ($in)
LEFT JOIN `bookmarked` c 
       ON c.app_id = a.id
      AND c.mid = b.mid         
LEFT JOIN `resale` d 
       ON d.app_id = a.id
      AND d.mid = b.mid 
LEFT JOIN `likes_count` e
       ON e.app_id = a.id
      AND e.mid = b.mid
    WHERE a.app_id = ?
)TAB2
ON TAB1.model_no = TAB2.model_no

";

    $users = $dbh->prepare($sql);
    $i = 1;
    foreach ($id_group as $id) {
      $users->bindValue($i++, $id);
    }

    $results = array_merge($id_group,$id_group);  

    $lasti = (count($results) + 1);

    $user_id = $_SESSION["user_id"];

    $users->bindValue($lasti,$user_id);

    $mergearray = array_merge($results,array($user_id));

    $users->execute($mergearray);

    $datas = array();
    $rows = $users->fetchAll(PDO::FETCH_ASSOC);
    foreach($rows as $row ) 
    {

      $data = array(
          "user_id"=> $row["app_id"],
          "ulike_type"=> $row["like_type"],
          "uaverage_min"=> $row["min"],
          "uaverage_max"=> $row["max"],
          "ufav"=> $row["isbooked"],
          "model_no"=> $row["model_no"],
          "likes"=> $row["likes"],
          "dislikes"=>$row["dislikes"],
          "average_min"=> $row["avg_min"],
          "average_max"=> $row["avg_max"],
          "average_users"=> $row["users_no"],

      );

      $datas[] = $data;
    }
     echo ($callback ? $callback . '(' : '') . json_encode($datas) . ($callback ? ')' : '');

    $dbh = null;

  }
  catch(PDOException $e) 
  {
    echo $e->getMessage(); 
  }

} 

?>

JSON 输出示例:

[
  {
    "user_id": "111",
    "ulike_type": null,
    "uaverage_min": null,
    "uaverage_max": null,
    "ufav": null,
    "model_no": "AAAA",
    "likes": "0",
    "dislikes": "0",
    "average_min": null,
    "average_max": null,
    "average_users": "0"
  },
  {
    "user_id": "222",
    "ulike_type": "0",
    "uaverage_min": "398",
    "uaverage_max": "823",
    "ufav": null,
    "model_no": "BBB",
    "likes": "0",
    "dislikes": "1",
    "average_min": "398.0",
    "average_max": "823.0",
    "average_users": "1"
  }
]

Print_r($datas) 的输出

Array
(
    [0] => Array
        (
            [user_id] => 111
            [ulike_type] => 
            [uaverage_min] => 
            [uaverage_max] => 
            [ufav] => 
            [model_no] => AAAA
            [likes] => 0
            [dislikes] => 0
            [average_min] => 
            [average_max] => 
            [average_users] => 0
        )

    [1] => Array
        (
            [user_id] => 222
            [ulike_type] => 0
            [uaverage_min] => 398
            [uaverage_max] => 823
            [ufav] => 
            [model_no] => BBB
            [likes] => 0
            [dislikes] => 1
            [average_min] => 398.0
            [average_max] => 823.0
            [average_users] => 1
        )
)

最佳答案

您将其定义为average_users

键值对的左侧是您用来寻址右侧的名称。

关于javascript - 从 JSON 编码获取控制台中 undefined object ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25164482/

相关文章:

javascript - 如果页面在中间滚动,则隐藏固定顶部标题

php - 在 PHP 函数中传递变量

javascript - Angular2 切换/打开 ng2-select 下拉菜单

javascript - 使用 jQuery Animate 进行页面转换,同时修复封面图像

javascript - Yii 2 不从 Javascript 读取 Cookie 集

javascript - 使用网络套接字检测页面退出

javascript - 如何使图像停留在鼠标悬停在它自己的位置

php - DB(MySQL) 用户帐户与用户表 - php

php - MySQL多SELECT查询成表单然后UPDATE

Jquery 回调函数没有改变 css 属性