Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching data from local JSON File in angularjs

Tags:

I want to fetch data from JSON file which is on my local machine. But I am not able to get the data. It is showing some cross domain error for $http.

Here is my code.

angular.module('myApp',[])   .controller('myCtrl', function ($scope, webtest) {      webtest.fetch().then(function (data) {          $scope.accounttype = data;      }) });  .factory('webtest', function($q, $timeout, $http) {     var Webtest = {         fetch: function(callback) {             return $timeout(function() {                 return $http.get('webtest.json')                 .then(function(response) {                       return response.data;                 });             }, 30);          }     };     return Webtest; });  

Anyone please help me how to display data from local JSON file? Thanks in Advance.

like image 374
Amit Avatar asked Nov 17 '15 09:11

Amit


People also ask

How to fetch data from local JSON file in AngularJS?

factory('webtest', function($q, $timeout, $http) { var Webtest = { fetch: function(callback) { return $timeout(function() { return $http. get('webtest. json') . then(function(response) { return response.


1 Answers

It's very simple like

$http.get('phones/phones.json').then(function(response) {    $scope.phones = response.data; }); 

Refer:http://stackoverflow.com/questions/21589340/read-local-file-in-angularjs

like image 105
Arun Avatar answered Nov 07 '22 23:11

Arun