1

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]"
}
];
8
  • what is the log of console.log(response) ? Commented Aug 6, 2017 at 14:11
  • it's not getting triggered Commented Aug 6, 2017 at 14:12
  • any errors in console? Commented Aug 6, 2017 at 14:14
  • i am getting error: Commented Aug 6, 2017 at 14:14
  • what error please explain? Commented Aug 6, 2017 at 14:15

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

see in my answer I have removed that too, and provided you a workable JSON... :)
if you feel that really helps then mark it as accepted in order to help others (including me :P )
i did since i am a new user it wont be counted
0

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]"}]

Comments

0

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.

8 Comments

what was the exact failure message/log in this case?
Error: [$http:baddata] Data must be a valid JSON object. Received: "[{"id":1}". Parse error: "{}" errors.angularjs.org/1.6.4/$http/… at angular.js:66 at defaultHttpResponseTransform (angular.js:11177) at angular.js:11270 at forEach (angular.js:403) at transformData (angular.js:11269) at transformResponse (angular.js:12090) at processQueue (angular.js:16832) at angular.js:16876 at Scope.$digest (angular.js:17971) at Scope.$apply (angular.js:18269) "Possibly unhandled rejection: {}"
i tried this for only {"id":1} in a new test.json but still the same error
then wrap it in a object
and are you sure that you putting the path correctly? what's the network tab says in the devTools/firebug?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.