【问题标题】:Ionic/Angular App not reading json data from apiIonic/Angular App 未从 api 读取 json 数据
【发布时间】:2015-06-29 01:16:37
【问题描述】:

我正在编写一个 Ionic 应用程序来从我用 asp.net MVC4 创建的 Web API 中读取一些数据。当我在本地机器上托管 Ionic 应用程序时,它可以很好地读取数据,但是当我在 Azure 上发布时尝试从 Web API 读取时,角度工厂不会返回任何内容。当它发布到 Azure 时,这有什么逻辑上的原因吗?

当我访问本地站点和天蓝色站点时,json字符串是相同的。

[{"ID":1,"Name":"Fireworks","Time":"2015-11-30T21:00:00","Comments":"Be there by 8:00"},
{"ID":2,"Name":"Haircut","Time":"2015-11-30T21:00:00","Comments":"Be there by 8:00"}]

这是我的角度工厂的代码

.factory('EventsFactory', ['$http', function($http) {
  return $http.get('webAPI link here')
      .success(function(data) {
        return data;
      })
      .error(function(err) {
        return err;
      });
}])

这是我的角度控制器的代码

.controller('EventCtrl', ['$scope', 'EventsFactory', function($scope,     EventsFactory) {
    EventsFactory.success(function(data){
        $scope.activities = data;
    });
}])

这是我的观点的代码

  <div class="list card" ng-controller="EventCtrl">
      <div class="item item-divider">Upcoming Events</div>
      <div class="item item-body" ng-repeat="activity in activities">
        <div>{{activity.Name}}</div>
      </div>
  </div>

【问题讨论】:

  • 当您查看浏览器的网络选项卡时,请求是否正确发送?它会返回一些东西吗?

标签: angularjs asp.net-mvc-4 ionic


【解决方案1】:

为什么要在 Controller 和 factory 中添加成功方法。仅添加成功方法控制器。喜欢

.factory('EventsFactory', ['$http', function($http) { return $http.get('webAPI link here'); }])

【讨论】:

    【解决方案2】:

    在你的控制器中,你需要使用then

    EventsFactory().then(function(data){
                    $scope.activites = data;
    })
    

    【讨论】:

      猜你喜欢
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多