0

I have a local json file (data.json) that I am attempting to parse using angular.fromJson which I don't have much familiar using. I've been following this post How do I update/add to a json file. My data.json file is in the same folder as my app.js file yet I am receiving a 404 error message 'localhost:3000/data.json 404 (Not Found)', which makes sense because data.json is not at that address it's at localhost:3000/apps/javascripts/data.json. Do I have to use an absolute url? However when I place data.json directly into angular.fromJson it works.

This works:

$scope.menu = angular.fromJson('{
      "name": "Mark",
      "id": 1,
      "project": "AMI",
      "project start": "10/1/2007",
      "project end": "9/31/2008"
    },
    {
      "name": "Ann",
      "id": 2,
      "project": "CLN",
      "project start": "10/1/2007",
      "project end": "9/31/2009"
    },
    {
      "name": "Mary",
      "id": 3,
      "project": "CAN",
      "project start": "10/1/2008",
      "project end": "9/31/2011"
    }');
  console.log($scope.menu);
        });

This doesn't work:

$http.get('data.json').success(function(data){
  $scope.menu = angular.fromJson(data.menu);
  console.log($scope.menu);

data.json script:

{   
"menu": [
      {
      "name": "Mark",
      "id": 1,
      "project": "AMI",
      "project start": "10/1/2007",
      "project end": "9/31/2008"
    },
    {
      "name": "Ann",
      "id": 2,
      "project": "CLN",
      "project start": "10/1/2007",
      "project end": "9/31/2009"
    },
    {
      "name": "Mary",
      "id": 3,
      "project": "CAN",
      "project start": "10/1/2008",
      "project end": "9/31/2011"
    }
    ]
}
4
  • What happens when you give the full path. Does that reject? Commented Aug 20, 2016 at 1:14
  • @Gary I get an error menu is not defined... i've added my json script above Commented Aug 20, 2016 at 1:27
  • @Gary yep I got it. Thanks for your help! Commented Aug 20, 2016 at 2:45
  • :-) thank christian. Have a nice day Commented Aug 20, 2016 at 4:37

1 Answer 1

1

Try changing the line

$http.get('data.json').success(function(data){

to

$http.get('/apps/javascripts/data.json').success(function(data){.

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

1 Comment

Thanks Christian :)

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.