Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have just started learning angularjs, I'm trying to load data from my json file on view. json file has a list of houses. But does not get showed on my view when I load the index.html file.

Data.json

      [
{
    "type": "Condo",
    "price": 220000,
    "address": "213 Grove Street",
    "description": "Excellent place! Really nice view!"
},
{
    "type": "House",
    "price": 410500,
    "address": "7823 Winding Way",
    "description": "Beautiful home with lots of space for large family."
},
{
    "type": "Duplex",
    "price": 395000,
    "address": "834 River Lane",
    "description": "Great neighourhood and lot's of nice green space."
},


]

cribsFactory.js

    angular
   .module('ngCribs')
   .factory('cribsFactory', function($http) {

    function getCribs() {

        return $http.get('data/data.json');
    }

    return {
        getCribs: getCribs
    }
     });

cribsController.js

     angular
       .module('ngCribs')
       .controller('cribsController', function($scope, cribsFactory) {

   $scope.cribs;

   cribsFactory.getCribs().success(function(data) {
        $scope.cribs = data;
    }).error(function(error) {
       console.log(error);
   });

    });

index.html

       <!doctype html>
       <html>
       <head>
       <meta charset="utf-8">
        <title>ng-cribbs</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
        </head>
        <body ng-app="ngCribs" ng-controller="cribsController">
            <div class="well" ng-repeat="crib in cribs">
                <h3>{{ crib.address }}</h3>
                <p>
                    <strong>Type: </strong>{{ crib.type }}</p>
                <p>
                    <strong>Description: </strong>{{ crib.description }}</p>
                <p>
                    <strong>Price: </strong>{{ crib.price | currency }}</p>
            </div>
        </body>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"/>
    </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.3/ui-bootstrap.min.js"/>
    </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.3/ui-bootstrap-tpls.min.js"/>
</script>
<script src="app.js"/>
</script>
<script src="scripts/cribsController.js"/>
 </script>
 <script src="scripts/cribsFactory.js"/>
  </script>
  </html>

I'm trying to run the above code in htdocs folder of XAMPP. The folder structure is in the screen shot below.

Screen shot

The json file is inside the data folder and the files cribsFactory.js & cribsController.js are in scripts folder. When i type the URL "http://localhost/ng-cribbs/" in Firefox the output is a completely blank page with no error message of any sort hence the difficulty in debugging.

I was able to load data using an array but facing problem while using JSON file.. Can't understand what I'm doing wrong. Please help!!

share|improve this question
1  
Possible duplicate of AngularJS: factory $http.get JSON file – Ankh Dec 7 '15 at 15:47
    
What is the error? – Shaohao Lin Dec 7 '15 at 15:52
    
@ShaohaoLin I have edited my question for your reference.. Please have a look.. – Lucy Dec 7 '15 at 15:57
2  
According to the screen shot you posted, your data.json is in the same level as the data folder. Wasn't data.json supposed to be under the data folder? – Jodevan Dec 7 '15 at 16:02
    
@Jodevan I put it there after the json file inside data folder did not generate any output. – Lucy Dec 7 '15 at 16:06
up vote 1 down vote accepted

Your json data was not valid, validate any json data before and you can use the below corrected json. Hope it helps! validate json here

     [{
    "type": "Condo",
    "price": 220000,
    "address": "213 Grove Street",
    "description": "Excellent place! Really nice view!"
  }, {
    "type": "House",
    "price": 410500,
    "address": "7823 Winding Way",
    "description": "Beautiful home with lots of space for large family."
  }, {
    "type": "Duplex",
    "price": 395000,
    "address": "834 River Lane",
    "description": "Great neighourhood and lot's of nice green space."
  }]
share|improve this answer
    
Thanks a lot for your help!! It seems that the comma before the closing square bracket was causing problem. After I removed it I'm getting the output correctly.. – Lucy Dec 8 '15 at 3:30
    
Yes that trailing comma caused all problem! Rest everything is cool with code. – anshulix Dec 8 '15 at 20:09

Your http call gives you a json string, you should convert it to a javascript array like so: $scope.cribs = JSON.parse(data)

share|improve this answer
    
What? No. Using $http will automatically return an array.. – Ankh Dec 7 '15 at 16:22
    
@GuenterGuckelsberger Tried this before posting question on SO. I didn't work.. – Lucy Dec 7 '15 at 16:25
1  
I just see, the data.json above is not valid json, the last , is wrong, but maybe this is just an extract of your original data file. – Guenter Guckelsberger Dec 7 '15 at 16:35
    
@GuenterGuckelsberger The error was indeed due to invalid JSON. Extra comma after the closing square bracket was causing the error. Removing it solved the problem.. – Lucy Dec 8 '15 at 3:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.