Fetching data from local JSON File in angularjs

后端 未结 3 1962
爱一瞬间的悲伤
爱一瞬间的悲伤 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).

提交回复
热议问题