AngularJS json url无法正常工作

时间:2016-05-05 23:04:59

标签: javascript angularjs json controller

有人可以看看我的js小提琴,我正试图让角度JS在JSON文件中显示列表,但我无法让它工作。

角度JS非常新,并试图在我走的时候弄明白

https://jsfiddle.net/2zumpvy9/

<body ng-app="DraftApp">
  <div class="main" ng-controller="HomeController">
    <div class="container">

      <div class="each" ng-repeat="player in players">
        {{ player.Player }}
      </div>

    </div>
  </div>
</body>


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

app.factory('players', ['$http', function($http) {
  return $http.get('http://redraft.comxa.com/test.json')
    .success(function(data) {
      return data;
    })
    .error(function(err) {
      return err;
    });
}]);


app.controller('HomeController', ['$scope', 'players', function($scope, players) {
  players.success(function(data) {
    $scope.players = data;
  });
}]);

1 个答案:

答案 0 :(得分:0)

但是,您可以使用https://crossorigin.me/服务。

然后,您应该请求:«https://crossorigin.me/http://redraft.comxa.com/test.json»。最后,这可以在AngularJS代码中轻松使用。

我使用AngularJS服务和控制器进行了演示。

var app = angular.module("DraftApp", []);

app.factory("players", ["$http",
  function($http) {
    return $http.get("https://crossorigin.me/http://redraft.comxa.com/test.json")
      .success(function(data) {
        return data;
      })
      .error(function(err) {
        return err;
      });
  }
]);

app.controller("HomeController", ["$scope", "players",
  function($scope, players) {
    players.success(function(data) {
      $scope.players = data;
    });
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="DraftApp">
  <div class="main" ng-controller="HomeController">
    <div class="container">

      <div class="each" ng-repeat="player in players">
        {{ player.Player }}
      </div>
    </div>
  </div>
</body>