David Biga
David Biga

Reputation: 2801

Posting data to JSON - Angular .post

I am working on an application and am having an issue posting to a .JSON file in my assets. I am working with an Angular based application. The error code I get is 404 with a response of: Cannot POST /assets/data/card-stack.json. Now the problem is, when I work with my get to retreive the JSON data it works perfect. It is only when I am using .post. Here is what I am doing:

$http.get('./../../assets/data/card-stack.json').success(function(data) {
                $scope.cards = data;
                // Set the showdown images from the card data grabbed from the card-stack.json file
                $scope.showdowns = [
                    $scope.cards[0].url,
                    $scope.cards[1].url,
                    $scope.cards[2].url,
                    $scope.cards[3].url
                ];
        });
        // Simple POST request example (passing data) :
        $http.post('./../../assets/data/card-stack.json', {url : './../images/banana.jpg'}).
          success(function(data, status, headers, config) {
            // this callback will be called asynchronously
            // when the response is available
          }).
          error(function(data, status, headers, config) {
              console.log(data);

            // called asynchronously if an error occurs
            // or server returns response with an error status.
         });

Suggestions?

Upvotes: 0

Views: 743

Answers (2)

Suneet Bansal
Suneet Bansal

Reputation: 2702

Try to set the $http header:

$http.defaults.headers.post["Content-Type"] = "application/json";

Try it.

Upvotes: 0

Wayne Ellery
Wayne Ellery

Reputation: 7958

A .json file is a static file that just contains json data so you won't be able to post to it. Instead you would need to use a server side page or service such as php to process the posted data.

Upvotes: 1

Related Questions