2

Thank you in advance!

Here is my app.js file:

var app = angular.module("app", []);

app.controller("AppCtrl", function($http) {
    var app = this;
    $http.get("./users/users.json")
      .success(function(data) {
        app.people = data;
      })

app.addPerson = function(person){
        $http.post("./users/user.json",person).
        success(function(data,status,headers,config) {
            app.people = data;
          }).error(function (data, status, headers, config) {
                app.status = status;
            });
    }

})

and the index.html:

<body ng-app="app" ng-controller="AppCtrl as app">

              <input type="text" ng-model="app.person.firstName" />
              <input type="text" ng-model="app.person.lastName" />
              <input type="button" ng-click="app.addPerson(app.person)" />

              <ul>
                <li ng-repeat="person in app.people">
                    {{person.firstName}} {{person.lastName}}
                </li>
              </ul>

And the json file, with which $http.get("./users/users.json") works fine.

[
    {"firstName":"One","lastName":"Two"},
    {"firstName":"Three","lastName":"Four"}
]

when I try to post I get error:

POST http://localhost:9006/users/user.json 501 (Unsupported method ('POST')) 

In the Network tab it shows Request Payload:

{firstName:Five,lastName:Six}
firstName: "Five"
lastName: "Six

I am using python SimpleHTTPServer. Thanks again.

2 Answers 2

4

You can't post directly to a static JSON file. If you want to update the file, you'll need to create a web service endpoint on the server to accept the POST request and update the file.

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

1 Comment

Sorry, stuck in same problem but could not understand this. Specially this "you'll need to create a web service endpoint on the server". Could you please explain a bit or with code if required. Facing this problem from last 3 days. Any kind of help would be appreciated. Thanks :)
0

Totally agree with Anthony Chu. After coming across his answer I did some research and came across this NPM package for a local REST api - I followed this tutorial - happy days! Hope this helps.

2 Comments

Answered 2 years since the question's creation? OMG
Yes. I found your post helpful to the question anyways.

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.