简体   繁体   中英

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": "joe@cordis.us"
},
{
"firstName": "Kate",
"lastName": "Will",
"contactNumber": "244-838-1213",
"contactEmail": "kate@cordis.us"
}
];

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.

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

like:

[
   {
        "firstName": "Joe",
        "lastName": "Perry",
        "contactNumber": "444-888-1223",
        "contactEmail": "joe@cordis.us"
    },
    {
        "firstName": "Kate",
        "lastName": "Will",
        "contactNumber": "244-838-1213",
        "contactEmail": "kate@cordis.us"
    }
]

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.

将您的json文件更改为(您的json无效):

[{"firstName":"Joe","lastName":"Perry","contactNumber":"444-888-1223","contactEmail":"joe@cordis.us"},{"firstName":"Kate","lastName":"Will","contactNumber":"244-838-1213","contactEmail":"kate@cordis.us"}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM