Access JSON file in Angular.js

I want to access the json file in angular.js using the following code, but I cannot access the code due to the error below. Help will be appreciated. !!

module:

angular.module("demoApp", ['demoApp.factory','demoApp.controllers']); 

Factory:

 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; }); }); 

Error: In firefox:

  "Access to restricted URI denied" code: "1012" return logFn.apply(console, args) 

In Chrome: `

 Origin null is not allowed by Access-Control-Allow-Origin. 
0
source share
1 answer

In fact, I think you do not have a server.

So, you get your application through the file:// protocol.

But the file:// protocol cannot use http get methods for security reasons .

To solve your problem, I suggest you place your application on a simple web server (for example, Apache or wamp, xamp, lamp, etc.) or it is better to use grunt to create your application and run it on the test server via node.js

+7
source

Source: https://habr.com/ru/post/973967/


All Articles