本文介绍了angularjs $ http.post导致501不支持的方法(“POST”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
感谢你在前进!
下面是我的app.js文件:
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;
});
}
})
和中的index.html:
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>
和JSON文件,与 $ http.get(./用户/ users.json)
工作正常。
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
我使用python SimpleHTTPServer。再次感谢。
I am using python SimpleHTTPServer. Thanks again.
推荐答案
您不能直接发布到一个静态的JSON文件。如果您想更新文件,你需要在服务器上创建一个Web服务端点接受POST请求并更新文件。
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.
这篇关于angularjs $ http.post导致501不支持的方法(“POST”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!