data正确地返回了json数据的第0行,但是尝试访问关键字时,比如service和online返回了undefined。
 <script>
      $.ajax({
    url:"count.php",
    dataType:'json', 
    success: function(data, status){
      console.log(data[0].online);
    }
  });
    </script>count.php
<?php 
$data = array();
 $data[] = file_get_contents('https://xxxxxik.php?%20metod=get_count_new%20&service=pro1&apikey=sdss');
  $data[] = file_get_contents('https://xxxxxik.com/priemnik.php?%20metod=get_count_new%20&service=pro2&apikey=sdds');
  echo json_encode($data);
?>Console.log(数据)打印下面的json
[
  "{\"service\":\"pro1\",\"online\":91}",
  "{\"service\":\"pro2\",\"online\":0}"
]发布于 2019-04-28 21:42:05
数组data包含字符串。您可以使用Array.prototype.map()和JSON.parse()将其转换为对象数组。
$.ajax({
   url:"count.php",
   dataType:'json', 
   success: function(data, status){
      data = data.map(x => JSON.parse(x))
      console.log(data[0].online)
   }
});发布于 2019-04-28 21:44:21
您需要先解析数据:
 <script>
      $.ajax({
    url:"count.php",
    dataType:'json', 
    success: function(data, status){
      data = JSON.parse(data);
      console.log(data[0].online);
    }
  });
    </script>https://stackoverflow.com/questions/55890814
复制相似问题