【问题标题】:AngularJS $http get method returns "Array" instead of the array's contentAngularJS $http get 方法返回“数组”而不是数组的内容
【发布时间】:2015-12-15 22:27:49
【问题描述】:

第一个请原谅我的英语我是法国人;)

尽管进行了大量网络研究,但我仍面临一个我无法理解的问题。我相信比我更熟练的人可以帮助我,因为我还是个学生。

我正在从 Angular 应用程序执行 http get,但返回的数据是字符串“Array”而不是数组的内容。 这是我的代码:

app.controller('WatchCtrl', function($scope, $http){
    $scope.getAll = function(){
      $http.get('http://www.myserver.com/videos/script.php')
        .then(function(response){
          $scope.videos = response.data;
          console.log(response); //to debug
        });
    }
    $scope.getAll();
  });

在服务器端,我的 php 脚本只是在做一个 echo ['a', 'b', 'c'] 以便测试。如果我回显一个像“hello”这样的字符串,我可以在客户端对其进行操作,但是只要这是一个数组,响应的数据属性就是“Array”。

控制台不显示任何错误消息。需要注意的是,我在脚本中放了一个 header("Access-Control-Allow-Origin: *") 以接受跨域。我不知道它是否在这种情况下发挥作用。

【问题讨论】:

  • echo ['a', 'b', 'c'] 是数组,因此服务器端提供文本 Array。试试json_encode([a,b,c]);

标签: php angularjs http get ionic


【解决方案1】:

你不能使用 echo 来渲染一个数组,所以在服务器端这样使用:

print json_encode(array('a','b','c')); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多