很难使用angular向JSON文件发出post请求
Having a hard time making a post request to a JSON file using angular
那里!对于Angular和Javascript来说,我是个新手,我正在尝试制作一个简单的一页玩具应用程序,在那里你可以使用JSON文件作为存储添加和查看客户。我已经能够显示我的JSON,但我一直在使用表单添加新条目。我一直在服务器上登录,当我点击提交时,不会发出发布请求。任何帮助都将不胜感激!谢谢
这是我的表格
<div ng-controller="CustomerAddController as addCtrl">
<form
ng-submit="addCtrl.addCustomer()" novalidate>
<h2>Add a new customer </h2>
<fieldset ng-model="addCtrl.customer.name">Name: <input type="text"/></fieldset>
<fieldset ng-model="addCtrl.customer.email">Email: <input type="email"/></fieldset>
<fieldset ng-model="addCtrl.customer.phone">phone: <input type="text"/></fieldset>
<h3>Address</h3>
<fieldset ng-model="addCtrl.customer.street">Street: <input type="text"/></fieldset>
<fieldset ng-model="addCtrl.customer.city">City: <input type="text"/></fieldset>
<fieldset ng-model="addCtrl.customer.state">State: <input type="text"/></fieldset>
<fieldset ng-model="addCtrl.customer.zip">Zip: <input type="text"/></fieldset>
<input type="submit" value="Submit"/>
</form>
</div>
这是我的app.js文件
'use strict';
var app = angular.module('app', [ ]);
app.controller('CustomerListController', function($scope, $http){
$http.get('customers.json').then(function(res){
$scope.customers = res.data;
});
});
app.controller('CustomerAddController', function($scope, $http){
$scope.addCustomer = function() {
$http.post('customers.json').then(function(res){
$scope.customers = res.data;
})
.success(function(data){
console.log(data);
if (!data.success) {
$scope.errorName = data.errors.name;
} else {
$scope.message = data.message;
}
});
};
});
你应该做的第一件事是,
将ng-model
指令附加到input
字段而不是fieldset
,因为它们只适用于input
&textarea
<form ng-submit="addCtrl.addCustomer()" novalidate>
<h2>Add a new customer </h2>
<fieldset>
Name: <input type="text" ng-model="addCtrl.customer.name" />
</fieldset>
<fieldset>
Email: <input type="email" ng-model="addCtrl.customer.email" />
</fieldset>
<fieldset>
phone: <input type="text" ng-model="addCtrl.customer.phone" />
</fieldset>
<h3>Address</h3>
<fieldset>
Street: <input ng-model="addCtrl.customer.street" type="text" />
</fieldset>
<fieldset>
City: <input ng-model="addCtrl.customer.city" type="text" />
</fieldset>
<fieldset>
State: <input ng-model="addCtrl.customer.state" type="text" />
</fieldset>
<fieldset>
Zip: <input ng-model="addCtrl.customer.zip" type="text" />
</fieldset>
<input type="submit" value="Submit" />
</form>
除此之外,因为您正在使用controllerAs
,所以控制器数据应该绑定到this
上下文。此外,post调用的URL中似乎有.json
,它应该是实际的服务器URL
,所以这些参数将与payload&正如@Kyle在下面的回答中指出的那样,不要忘记在post
请求中传递数据。
var app = angular.module('app', [ ]);
app.controller('CustomerListController', function($http){
var self = this;
$http.get('customers.json').then(function(res){
self.customers = res.data;
});
});
app.controller('CustomerAddController', function($http){
var self = this;
self.addCustomer = function() {
//below serverUrl would be server where you configured post request
//pass self.customer in that, will pass whole customer filled form
$http.post(serverUrl, self.customer).then(function(res){
self.customers = res.data;
})
.success(function(data){
console.log(data);
if (!data.success) {
self.errorName = data.errors.name;
} else {
self.message = data.message;
}
});
};
});
您需要使用$http.post()发送数据
$http.post("customers.json",data).then...
https://docs.angularjs.org/api/ng/service/$http
相关文章:
- 当我在form_for中的text_field_tag中点击Enter时,如何禁止发出POST请求
- 为什么 AJAX POST 请求不起作用
- Ajax POST请求没有'我不了解PHP
- 防止双击执行两次jQuery post请求
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 如何使用Javascript获取POST请求填充的元素的值
- NodeJS中POST请求的请求体为null
- 在Fiddler JavaScript中识别POST请求
- 使用application/x-www-form-urlencoded使用node.js在post请求中发送数组
- 打开一个选项卡,并在firefox网络扩展中向其发出POST请求
- 通过AJAX向同一页面发送POST请求,并使用$_POST获取值
- Ajax Post请求不起作用
- 如何在cloud9中向server.js发送post请求
- mootools表单json post请求
- 可以'无法在CasperJS中复制POST请求
- 使用用户名/API令牌javascript的Jenkins POST请求
- 在表单上注入javascript以发送post请求
- 使用javascript在post请求中传递用户输入变量
- 如何使用javascript进行post请求
- 如何在Javascript中通过POST请求发送XML数据