Fetching data from local JSON File in angularjs

后端 未结 3 1961
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-25 13:41

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

相关标签:
3条回答
  • 2020-12-25 13:53

    Don't you have an error message like "$http: is not defined" ?

    I tried with a controller, this is working :

    var ngApp = angular.module("ngApp", []);
    
    ngApp.controller('myController', ['$http', function($http){
      var thisCtrl = this;
    
      this.getData = function () {
      this.route = 'webtest.json';
      $http.get(thisCtrl.route)
        .success(function(data){
          console.log(data);
        })
        .error(function(data){
          console.log("Error getting data from " + thisCtrl.route);
        });  
      }
    }]);
    

    If you haven't, use web developer tools (Ctrl+Shift+I in firefox).

    0 讨论(0)
  • 2020-12-25 14:06

    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

    0 讨论(0)
  • 2020-12-25 14:13

    If you haven't already done so. Try setting up a crossdomain policy for your application.

    0 讨论(0)
提交回复
热议问题