Search code examples
jsonangularjshttpstatus

angular.js $http.error no status return json


I am new with Angular.js, and I am following the tutorial of the angular website.

I am trying to get my data from a json file with the $http service, but nothing happens!! Even the status of the error is not working...

Here is my code:

 <li>{{status}}</li>

 <ul class="messages">
      <li ng-repeat="message in messages">
            {{message.title}}
            <p>{{message.title2}} {{message.icon}}</p>    
      </li>
 </ul>

and the js part:

var monitorControllers = angular.module('monitorControllers', []);

monitorControllers.controller('MonitorCtrl', ['$scope', '$http',
    function($scope,$http) {
   $http.get('data/messages.json').success(function(data, status) {
       $scope.status = status;
       $scope.messages = data;
   }).error(function(data, status) {
           $scope.messages = data || "Request failed";
           $scope.status = status;
       });    

}]);

THE SOLUTION :

Thank you sylwester, I found the problem but it is really strange. So I just added in my js file, this: (at the beginning and the end..)

(function () {
    var monitorControllers = angular.module('monitorControllers', []);

    monitorControllers.controller('MonitorCtrl', ['$scope', '$http',
        function ($scope, $http) {

            $http.get('data/messages.json').success(function(data, status) {
             $scope.status = status;
             $scope.messages = data;
             }).error(function(data, status) {
             $scope.messages = data || "Request failed";
             $scope.status = status;
             });


        }]);
})();

Solution

  • More likely you missed something please see there :http://jsbin.com/canuma/1/edit?html,js,output

    html:

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
    <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
      <meta charset="utf-8">
      <title>JS Bin</title>
    </head>
    <body ng-app="monitorControllers">
      <div ng-controller="MonitorCtrl">
       <li>{{status}} {{messages}}</li>
    
     <ul class="messages">
          <li ng-repeat="message in messages">
                {{message.title}}
                <p>{{message.title2}} {{message.icon}}</p>    
          </li>
     </ul>
          </div>
    </body>
    </html>
    

    js:

    var monitorControllers = angular.module('monitorControllers', []);
    
    monitorControllers.controller('MonitorCtrl', function($scope, $http){
    
      $http.get('data/messages.json').success(function(data, status) {
           $scope.status = status;
           $scope.messages = data;
       }).error(function(data, status) {
               $scope.messages = data || "Request failed";
               $scope.status = status;
           });  
    
    
    });