2015-09-24 16 views
0

無法使用angularjs,我正在嘗試從JSON文件中的數據,這裏是我的代碼無法使用angularjs來從JSON文件數據

<!DOCTYPE html> 
<html> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> 
<body> 
<div ng-app="myApp" ng-controller="customersCtrl"> 

<ul> 
<li ng-repeat="x in names"> 
{{ x.eng_rev_type + ', ' + x.year_end }} 
</li> 
</ul> 

</div> 

<script> 
var app = angular.module('myApp', []); 
app.controller('customersCtrl', function($scope, $http) { 
$http.get("http://preview.sokrati.com/data/open_spendings.json") 
.success(function (response) {$scope.names = response.data;}); 
}); 
</script> 

</body> 
</html> 
+0

當你運行你的代碼時會發生什麼?你到底想要發生什麼?錯誤是什麼? – LionC

+0

數據不會從json文件中檢索 – ch3t

+0

嘗試'$ http.get(「http://preview.sokrati.com/data/open_spendings.json」).then(function(response){$ scope.names = response。數據;});});' – DonJuwe

回答

-1

嘗試$調用接受申請後,檢索JSON文件數據一個迴應。

if(!$scope.$$phase) $scope.$apply(); 
+0

不工作,http://plnkr.co/編輯/ PAiej6P5DzCoTmZA4oru?p =預覽 – ch3t

+0

我編輯了答案,但我認爲這主要是一個交叉來源問題,請檢查控制檯並查找是否存在訪問控制允許來源標題資源。 Origin' –

+0

是的,這是一個交叉來源的問題,那麼替代解決方案是什麼? – ch3t