0

This is my app

objectsApp.controller('ObjectsController', ['$http', '$scope', '$routeParams', 
function($http, $scope, $routeParams) {
        $http.post('1object.json', "my first write").then(function(data) {
        console.log(data)
    })
}]);

When run, this is the response (data)

Object {data: "", status: 200, headers: function, config: Object, statusText: "OK"}

However, there is no change to the file 1object.json (expected my first write)

The file 1object.json exists, but is just blank. I am running on a windows machine.

Why wouldn't this be writing?

4
  • 3
    POSTing to a static file does not automatically write to it. It depends on what your server is doing with the request. You would have to take the post body and write to that file explicitly. Commented May 5, 2015 at 15:49
  • @ExplosionPills That's what I assumed originally, angular being client-side, but a couple tutorials on the web recommend the above method Commented May 5, 2015 at 16:02
  • @ColinPalmer Just curious can you post a link to one of said tutorials? Commented May 5, 2015 at 16:09
  • Something either misinterpreted or faulty from those tutorials. .json files are static files with no dynamic language code processing. Have to use a server script in whatever language it is running to write to file ... and will find it simpler in the long run to use alternate storage like db than trying to index your own json files Commented May 5, 2015 at 17:30

1 Answer 1

2

If you are using Node.js you can use the fs module to get the functionality you want. In your server.js you would have a route which your angular $http.post directs to, and that route would use fs.writeFile and the posted data to write the .json file.

EDIT: I would like to mention, depending on what your use for the JSON file is, you may consider using a database like redis to store your JSON data.

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

Comments

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.