Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用angularjs从json文件检索数据_Json_Angularjs - Fatal编程技术网

无法使用angularjs从json文件检索数据

无法使用angularjs从json文件检索数据,json,angularjs,Json,Angularjs,无法使用angularjs从json文件检索数据,我正在尝试从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="customersC

无法使用angularjs从json文件检索数据,我正在尝试从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>

  • {x.eng_rev_type+','+x.year_end}
var-app=angular.module('myApp',[]); app.controller('customersCtrl',函数($scope,$http){ $http.get(“http://preview.sokrati.com/data/open_spendings.json") .success(函数(响应){$scope.names=response.data;}); });
收到响应后,尝试调用$apply

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

运行代码时会发生什么?你到底想发生什么?错误是什么?没有从json文件检索数据try
$http.get(“http://preview.sokrati.com/data/open_spendings.json)然后(函数(响应){$scope.names=response.data;});})您是否正在尝试执行跨站点请求?如果是这样,那么这将不起作用。错误:XMLHttpRequest无法加载。请求的资源上不存在“Access Control Allow Origin”标头。因此不允许访问源“”。我已编辑了答案,但我认为这主要是一个跨源问题,请检查控制台并查找请求的资源上是否不存在
任何“访问控制允许源”标题。Origin
是的,这是一个跨源问题,那么替代解决方案是什么呢?我认为您需要一个服务器解决方案来获取JSON数据,然后将其回显到客户端,因为JSONP不会与URL一起工作。