0

I have a php file which returns a json data. I am using angular http ajax. But I am unable to get my properly formatted json which i want to use in my angular js. Please have a look at my code:

file name: json.php

<?php

$data = array();

$data1 = array('name' => 'dheerendra', 'city' => 'bangalore');
$data2 = array('name' => 'kk', 'city' => 'delhi');
$data3 = array('name' => 'aakash', 'city' => 'kanpur');
$data4 = array('name' => 'amit', 'city' => 'bangkok');

$data[] = [$data1, $data2, $data3, $data4];
print json_encode($data);
?>

this is my app.js file for angular js:

filename: app.js

var calculator = angular.module('calculator', ['ngRoute', 'myController']);

calculator.factory('simpleFactory', function ($http) {
    var customers = [];

    var factory = {};

    factory.getCustomers = function () {
       return $http({method: 'GET', url: 'data/json.php'}).
                then(function successCallback(response) {
                    customers = response.data;
                    return customers;
                });
    };
    return factory;
});

this is my angular controller:

filename: controller.js

var myController = angular.module('myController', []);

myController.controller('controller1', function ($scope, simpleFactory) {
    $scope.customers = [];
    init();
    function init() {
        var myDataPromise = simpleFactory.getCustomers();
        myDataPromise.then(function (result) {
            // this is only run after getData() resolves
            $scope.customers = result;
            console.log($scope.customers );
        });
    }
});

this is my index.html:

<!DOCTYPE html>
<html ng-app="calculator">
    <head>
        <title>Calculator</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div ng-controller="controller1">            
          <ul>
            <li ng-repeat="customer in customers">
             {{customer.name}} - {{customer.city}}
            </li>
          </ul>
        </div>

        <script src="javascript/angular.min.js"></script>
        <script src="javascript/angular-route.min.js"></script>
        <script src="javascript/app.js"></script>
        <script src="javascript/controllers.js"></script>
    </body>
</html>

my console.log($scope.customers ) shows this :

Ok I saw that I have the data in console.log but how do I get those values which I am interested in ?

enter image description here

I want my customer array to have only these values so that I can print them in my li element:

var customer=[          
          {name: 'dheerendra', city: 'bangalore'},
          {name: 'kk', city: 'delhi'},
          {name: 'aakash', city: 'kanpur'},
          {name: 'amit', city: 'bangkok'},
        ];

I have created another same array in the file and I am logging both the arrays. Both of them seems to be different:

var myController = angular.module('myController', []);

myController.controller('controller1', function ($scope, simpleFactory) {
    $scope.customers = [];
    init();
    function init() {
        //$scope.customers = simpleFactory.getCustomers();
        var myDataPromise = simpleFactory.getCustomers();
        myDataPromise.then(function (result) {
            // this is only run after getData() resolves
            $scope.customers1 = [
                {name: 'dheerendra', city: 'bangalore'},
                {name: 'kk', city: 'delhi'},
                {name: 'aakash', city: 'kanpur'},
                {name: 'amit', city: 'bangkok'}
            ];
            $scope.customers = result;
            console.log($scope.customers1);
            console.log($scope.customers);
        });
    }
});

console output is as follows:

enter image description here

Top one is my hardcoded array and bottom one is what I got from http. Both of them seem to be different.

Found the solution. The problem was in my json.php file

$data[] = [$data1, $data2, $data3, $data4] ;

this line was creating an array of length one which contained array of four objects. Thats why customers = response.data; was not returning me those 4 objects.

The solution is to just remove square brackets in this line

$data = [$data1, $data2, $data3, $data4];
7
  • 1
    so whats the actual issue here? seems you have the data, is just the outputting it an issue? Commented Mar 27, 2016 at 14:11
  • Yes I have the data but I don't know how to get only the data which I am interested in . Commented Mar 27, 2016 at 14:14
  • Do you get an error message? Commented Mar 27, 2016 at 14:15
  • which data are you interested in bro? :) Commented Mar 27, 2016 at 14:16
  • 1
    @user2950720 .success is deprecated. See docs.angularjs.org/api/ng/service/$http#deprecation-notice Commented Mar 27, 2016 at 14:45

1 Answer 1

0

Looking at the console log. The XHR is returning an array of length one that contains an array of four objects.

Modify your factory to handle that.

calculator.factory('simpleFactory', function ($http) {
    var customers = [];

    var factory = {};

    factory.getCustomers = function () {
       return $http({method: 'GET', url: 'data/json.php'}).
                then(function successCallback(response) {
                    //Use This
                    customers = response.data[0];
                    //Instead of this
                    //customers = response.data;
                    return customers;
                });
    };
    return factory;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.