Shiv Gaurav
Shiv Gaurav

Reputation: 448

not getting data from JSON file using angularjs 1.6

I am trying to get data from my JSON file using AngularJs 1.6

  myApp.controller("homeCtrl", function($scope, $http) {
      $scope.Data = [];
      var getJsonData = function() {
          $http.get('contactlist.json').then(function(response) {
              $scope.Data = response.data;
              console.log(response.data);
          });
      }
      getJsonData();
  });

But it's not going to response I am putting debug on then line but my page opened without stopping on debug response. So it's not going on then(function(reponse){

My JSON File:

var contactList = [
{
"firstName": "Joe",
"lastName": "Perry",
"contactNumber": "444-888-1223",
"contactEmail": "[email protected]"
},
{
"firstName": "Kate",
"lastName": "Will",
"contactNumber": "244-838-1213",
"contactEmail": "[email protected]"
}
];

Upvotes: 1

Views: 489

Answers (3)

Shiv Gaurav
Shiv Gaurav

Reputation: 448

Got it resolved. Issue was because of semicolon at the end of json file data. Got this error when tried pasting in Plunker editor My Bad.

Upvotes: 1

Koushik Chatterjee
Koushik Chatterjee

Reputation: 4175

Remove var contactList = from you JSON file and place the JSON contents only

like:

[
   {
        "firstName": "Joe",
        "lastName": "Perry",
        "contactNumber": "444-888-1223",
        "contactEmail": "[email protected]"
    },
    {
        "firstName": "Kate",
        "lastName": "Will",
        "contactNumber": "244-838-1213",
        "contactEmail": "[email protected]"
    }
]

var contactList = <something> meant it's javascript code need to be execute, but you are reading the file and parsing it as a json data and not executing as js file, so make it like a json file so the contents is only json string and not some javascript code.

Upvotes: 0

Gaurav Srivastava
Gaurav Srivastava

Reputation: 3232

Change your json file to(your json is not valid):

[{"firstName":"Joe","lastName":"Perry","contactNumber":"444-888-1223","contactEmail":"[email protected]"},{"firstName":"Kate","lastName":"Will","contactNumber":"244-838-1213","contactEmail":"[email protected]"}]

Upvotes: 0

Related Questions