0

I have a factory that doesn't work:

toDoListApp.factory("blahFactory", function($http) {
    return {
        getChores : function() {
            return $http({
                url: '/chore.json',
                method: 'GET'
            })
        }
    }
});

And a chore.json file that has data:

{"name":"laundry","hours":"3"},{"name":"dishes","hours":"0.5"},{"name":"blah","hours":"1.5"}

And my controller uses this factory but breaks on that line:

toDoListApp.controller("ChoresController", function($scope, choresFactory, blahFactory) {

    blahFactory.getChores().success(function(data) {
        $scope.blah = data;
        console.log(data);
    });

    ....

The trace in my firebug console is not very descriptive: Error: JSON.parse: unexpected non-whitespace character after JSON data + a bunch of angular junk that does not make sense. Can anyone help explain? Is my JSON invalid?

=== UPDATE ===

Ok so I'm trying to put the array brackets into the json file and modified the $http.get function in my factory to use cache: false but it still doesn't update my json because it's cached...

=== ANSWER ===

So I deleted chrome's cache and added array brackets and everything worked!

1
  • 1
    Your json data dosen't seem properly formatted. Should be inside an Array. Commented Apr 20, 2014 at 21:35

1 Answer 1

3

Your JSON isn't correct. You just have three objects next to each other, instead, put those in an array like this.

[
  {"name":"laundry","hours":"3"},
  {"name":"dishes","hours":"0.5"},
  {"name":"blah","hours":"1.5"}
]

everything else looks great.

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

3 Comments

Oh ok. but another issue I think is that the browser keeps caching my json, so when I try to play around with it it never updates on the browser and I don't know if I'm fixing it or not. How do I stop the cache?
read the documentation. The $http service has a cache option. docs.angularjs.org/api/ng/service/$http
Correct, I would advise you grant this answer to @Tyler. Your question has been answered.

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.