2015-05-18 65 views
0

我的HTTP獲取不會加載數據,我不知道爲什麼。請有人能幫助我嗎?我已經嘗試了兩個虛擬主機,我的虛擬主機和我得到一個'沒有'訪問控制允許來源'標題出現在請求的資源。'錯誤,所以我用Myjson服務器,現在我沒有得到任何錯誤,但沒有加載,請有人可以幫忙嗎?Ionic:HTTP get不工作?

這是我的JSON文件:

{ 
    "response1": [ 
    { 
     "announcement_name": "Example Message 1" 
    }, 
    { 
     "date": "15/05/2015" 
    }, 
    { 
     "message": "Example Message 1" 
    } 
    ], 
    "response2": [ 
    { 
     "announcement_name": "Example Message 2" 
    }, 
    { 
     "date": "15/05/2015" 
    }, 
    { 
     "message": "Example Message 2" 
    } 
    ] 
} 

HTML:

<body ng-app="Announcement"> 
    <ion-view view-title="Announcements"> 
     <ion-pane> 
     <ion-header-bar class="bar-positive"> 
      <h1 class="title">Pull To Refresh</h1> 
     </ion-header-bar> 
     <ion-view view-title="Announcements"> 
     <ion-content ng-controller="Controller"> 
      <ion-refresher on-refresh="doRefresh()"> 

      </ion-refresher> 
      <ion-list> 
      <ion-item ng-repeat="item in items" ng-click="getData()"> 

      <div class="list card"> 
       <div class="item item-divider">{{announcement_name}} - {{date}}</div> 
       <div class="item item-body"> 
       <div> 
        {{message}} 
       </div> 
      </ion-item> 
      </ion-list> 
     </ion-content> 
     </ion-view> 

的Javascript:

.controller('Controller', function($scope, $http) { 
    $scope.items = [1,2,3]; 
    $scope.doRefresh = function() { 
     $http.get("https://api.myjson.com/bins/1sdnx") 
      .success(function(data) { 
       $scope.announcement_name = data.announcement_name; 
       $scope.date = data.date; 
       $scope.message = data.message; 
      }) 
      .finally(function() { 
      // Stop the ion-refresher from spinning 
      $scope.$broadcast('scroll.refreshComplete'); 
     }); 
     }; 
    }); 
+0

您的應用程序是否連接到互聯網?您可能必須將科爾多瓦Android 4.0及更高版本的域列入白名單。在這種情況下,這將幫助你:http://stackoverflow.com/a/29916802/4412363 – Keval

+0

那麼在運行時數據的價值是什麼? – QueryLars

回答

0

實際響應數據,存在響應的data對象。

.controller('Controller', function($scope, $http) { 
    $scope.items = [1,2,3]; 
    $scope.doRefresh = function() { 
     $http.get("https://api.myjson.com/bins/1sdnx") 
      .success(function(result) { 
       console.log(result); 
       console.log(result.data); 
       $scope.announcement_name = result.data.announcement_name; 
       $scope.date = result.data.date; 
       $scope.message = result.data.message; 
      }) 
      .finally(function() { 
      // Stop the ion-refresher from spinning 
      $scope.$broadcast('scroll.refreshComplete'); 
     }); 
     }; 
    });