0

I am having difficulty getting $http service to work with my AngularJS application. I am developing it in Visual Studio 2012, using IISExpress. I have the following code:

var myApp = angular.module('myApp', []);
myApp.controller('MyController', ['$scope', '$http', function($scope,$http) {                    
  $http.get('js/data.json').success(function(data) {
    $scope.artists = data;
  });
}]);

"success" is never fired. How do I get $http to work?

shilovk
  • 11,718
  • 17
  • 75
  • 74
dars7
  • 3
  • 2
  • Have you examined the network tab in developer tools to see what's happening to the request? This is one of the first things you should do before posting an SO question. – adaam Jul 09 '15 at 14:20
  • Have you tried to start the path to the json file with a dot? "./js/data.json"? – Frode Jul 09 '15 at 14:21
  • Without knowing more than you have here, my guess would be that this code is on a page that is not in the root folder of the website and the folder `js`, that contains your JSON data, is. If this is so then the `$http.get('js/data.json')` is wrong and you'll need to prefix your url with a leading `/` i.e. `$http.get('/js/data.json')` – phuzi Jul 09 '15 at 14:30

1 Answers1

1

You should also use .error() to figure out why it's not working.

angular
    .module('myApp', []);

angular
    .module('myApp')
    .controller('MyController', ['$scope', '$http',
        function($scope, $http) {                    
            $http.get('js/data.json')
                .success(function(data) {
                    $scope.artists = data;
                })
                .error(function(err) {
                    // Figure out what's not working here.
                    console.log(err);
                });
        }
    ]);
kiswa
  • 14,737
  • 1
  • 21
  • 29
  • an error was generated. actually an entire html error page...part of it says this: This error occurs when the file extension of the requested URL is for a MIME type that is not configured on the server. You can add a MIME type for the file extension for files that are not dynamic scripting pages, database, or configuration files. Process those file types using a handler. You should not allows direct downloads of dynamic scripting pages, database or configuration files – dars7 Jul 09 '15 at 15:22
  • Then you need to setup IIS to allow direct downloads of `.json` files. http://stackoverflow.com/questions/8158193/how-to-allow-download-of-json-file-with-asp-net – kiswa Jul 09 '15 at 15:23
  • That was the problem. Thanks. – dars7 Jul 09 '15 at 15:29