duanjuelu8874 2016-03-27 14:00
浏览 37

如何从角度JS的php文件中获取json数据

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];
  • 写回答

1条回答 默认 最新

  • dpp34603 2016-03-27 14:51
    关注

    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;
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值