Reputation: 33
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.
Upvotes: 2
Views: 2178
Reputation: 307
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.
Upvotes: 0
Reputation: 37520
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.
Upvotes: 4