[英]Access JSON file in Angular.js
我想使用以下代碼訪問angular.js中的json文件,但由於以下提到的顯示錯誤,我無法訪問該代碼。 幫助將不勝感激!!
模塊:
angular.module("demoApp", ['demoApp.factory','demoApp.controllers']);
廠:
angular.module('demoApp.factory', []).
factory('getJsonDataFactory',function($http){
return{
getData : function() {
return $http.get('mapData.json');
}
}
});
控制器:
angular.module('demoApp.controllers', []).
controller('GetJsonController', function($scope,$http,getJsonDataFactory) {
$scope.markers = [];
getJsonDataFactory.getData().success(function(data){
$scope.photos=data;
});
});
錯誤:在Firefox中:
"Access to restricted URI denied" code: "1012"
return logFn.apply(console, args)
在Chrome中:
Origin null is not allowed by Access-Control-Allow-Origin.
實際上,我認為您沒有服務器。
因此,您正在通過file://
協議獲取應用程序。
但是出於安全原因, file://
協議不能使用http get方法 。
為了解決您的問題,我建議您將應用程序放在簡單的Web服務器上(例如Apache,wamp,xamp,lamp等),或者最好使用grunt來構建應用程序並通過node.js在測試服務器上運行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.