Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This my php code

    <?php


try {
    $dbcon=new PDO('mysql:host=localhost;dbname=angular;charset=utf8',"root","");

    $dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbcon->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
    $query="SELECT * FROM products ";
    $sql=$dbcon->prepare($query);
    $sql->execute();
    $result=$sql->fetchAll(PDO::FETCH_OBJ);

    $json_result=json_encode($result);

    echo $json_result;

}catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}


?>

and this my controller to angular

  function ProductListCtrl($http)
{

    $http.get('api/products.php').success(function (data) { alert(data); this.product = data; });

}

the alert message is [object Object,...] ,how can i retrieve the json data from php?

share|improve this question
2  
That is the json object, if you want to alert it as a string you probably want JSON.stringify(data). Otherwise in your code you should be able to just access the properties (i.e. data.foo.bar) – nerdwaller Nov 21 '14 at 18:31
    
the problem is i think that $http.get do not retrieve json data ,for some reason from the php script. – user1658429 Nov 21 '14 at 18:48
    
Which is why I have a solution to check that, it'll be null, an empty array, or empty object in your alert if it didn't succeed. Another easy way to check is to run curl http://host:port/path – nerdwaller Nov 21 '14 at 20:31

$http.get work fine with JSON I recommend you to validate your json and test again I created a simple example for you with $http and ng-repeat

<!doctype html>
<html ng-app="test">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
    <script>
      var app = angular.module('test', []);
      app.controller('test', function($scope, $http) {
        $http.get('test.json').success(function(data){
          console.log(data);
          $scope.items = data;
        });
      });
    </script>
  </head>
  <body ng-controller="test">
    <div ng-repeat="item in items">{{item.name}}</div>
  </body>
</html>

and the json file is

[{
  "name":"name1"
},
{
  "name":"name2"
}]

and also a link to watch it live in Plunker http://plnkr.co/edit/MN6UPg1ba1OYNFNHKMdG?p=preview

share|improve this answer

I solved the problem.The wrong is with this.product

(function ()
{
    "use strict";
   var app = angular.module('ProductList');
   app.controller('ProductListCtrl',['$scope','$http', ProductListCtrl]);

   function ProductListCtrl($scope, $http) {

       $http.get('api/products.php').success(function (data) { $scope.product = data; });




   }

}());
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.