2015-09-01 21 views
0

我創建了一些json文件,我試圖用我的角度訪問。

我的JSON的樣子:

[ 
    {"name": "China", "population": 12313214314}, 
    {"name": "USA", "population": 97888945}, 
    {"name": "Germany", "population": 456789}, 
    {"name": "Uzbekistan", "population": 18877}, 
    {"name": "Tadžikistan", "population": 499999}, 
    {"name": "Turkistan", "population": 111111} 
] 

所以很簡單的對象,其中被僅舉國家和一些虛人口的列表。

我在上面創建一個控制器,loooks像:

// Somewhere on a top is this line 
var app = angular.module('myApp', []); 
// And controller here 
     app.controller('CountryJsonCtrl', function($scope, $http){ 
      $http.get('countries.json').success(function(data){ 
       $scope.counties = data; 
      }) 
     }) 

在它看起來像一個HTML:

<div ng-controller="CountryJsonCtrl"> 
    <table> 
     <tr> 
      <th>Country</th> 
      <th>Population</th> 
     </tr> 
     <tr ng-repeat="country in countries"> 
      <td>{{country.name}}</td> 
      <td>{{country.population}}</td> 
     </tr> 
    </table> 
</div> 

所以我創建了一個表的每一行應在通過NG新線-重複。 Dispite這一事實的國家沒有在APP加載所以我期待到瀏覽器控制檯,我得到這個錯誤:

XMLHttpRequest cannot load file:///C:/AngularJS/countries.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. 

能someboy幫我在哪裏是一個問題,這或解釋é什麼,我做錯了什麼? Beause我只是beginer的角度我任何並欣賞提醒


編輯: @喬恩雪的提醒後,我在本地主機上運行它,結果是出現在控制檯沒有更多的錯誤,我看到JSON加載在網絡標籤,但仍然沒有結果在網站上。

回答

2

由於您直接打開HTML文件,因此您不允許執行HTTP請求。你需要建立一個本地服務器。

+0

感謝adivse,是像XAMPP與阿帕奇enaugh嗎?還是我需要別的東西? – Andurit

+0

是的XAMPP/WAMP應該做的很好。 –

+0

很酷,我只是在localhost上運行它,@Jon Snow我可能會問你檢查我的編輯嗎? – Andurit