duanbiaojin8860 2015-10-06 05:41
浏览 70

php将json传递给angularjs

this is my php code,

    $count=mysql_num_rows($query);

if ($count > 0) {
  // output data of each row
  $foodList[] = array();
  while($row =mysqli_fetch_assoc($result))
  {
      $foodList[] = $row;
  }
} else {}

echo json_encode($foodList);

this is my js code:

var $promise = $http.post('foodList.php');

$promise.then(function(msg){
  var foodList = msg.data;

  if (foodList) 
    {
      //$scope.foodList = foodList;
      alert(foodList);
    }
  else
    {
      //$scope.msg = "Error user name or password";
    }

this is output:

$promise.then(function(msg){*msg = Object {data: Array[1], status: 200, config: Object, statusText: "OK"}* var foodList = msg.data;*foodList = [Array[0]]*

So: actually 3 data in my data base, but in output just only Array[1]? How to fix it ? THX

  • 写回答

2条回答 默认 最新

  • dsl2014 2015-10-06 06:17
    关注

    Your php syntax and the usage of commands are wrong in some places. Here is the corrected code. Please compare and see the difference.

    $count=mysql_num_rows($result);
    
    if ($count > 0) {
    
      // output data of each row
      $foodList = array();
    
      while($row =mysqli_fetch_assoc($result))
      {        
          array_push($foodList, $row);
      }
    }
    
    echo json_encode($foodList);
    

    This should work if you are selecting the rows correctly.

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题