简体   繁体   English

使用angular很难向JSON文件发出发布请求

[英]Having a hard time making a post request to a JSON file using angular

there! 那里! Pretty new to Angular and Javascript in general, I'm trying to make a simple one page toy app where you can add and view customers using a JSON file as storage. 通常,对于Angular和Javascript来说,这是一个新手,我正在尝试制作一个简单的单页玩具应用程序,您可以在其中使用JSON文件作为存储来添加和查看客户。 I've been able to get my JSON to display but I've gotten stuck on adding new entries using a form. 我已经能够显示我的JSON,但是我一直坚持使用表单添加新条目。 I've been checking in on the server and when I click submit not post request is made. 我一直在服务器上签入,当我单击“提交”时,未发出发布请求。 Any help would be much appreciated! 任何帮助将非常感激! thanks! 谢谢!

this is my form 这是我的表格

<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>

and this is my app.js file 这是我的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;
          }
      });

    };
});

Very 1st thing you should to is, 您应该做的第一件事是

Attach ng-model directive to input fields not to fieldset , as they are only tends to work with input & textarea ng-model指令附加到input字段而不附加到fieldset ,因为它们仅倾向于与inputtextarea

<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>

Other than that as you are using controllerAs so controller data should bounded to this context. 除了使用controllerAs ,控制器数据还应绑定this上下文。 Also post calls seems to have .json in URL, which should be actual server URL , so those parameter will get passed with payload & don't forget to pass data in post request as @Kyle pointed out in below answer. 另外,post调用似乎在URL中.json ,它应该是实际的服务器URL ,因此这些参数将随有效负载一起传递,并且不要忘记在post请求中传递数据,如@Kyle在以下答案中指出的那样。

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;
          }
      });
    };
});

You need to send data with your $http.post() 您需要使用$ http.post()发送数据

$http.post("customers.json",data).then...

https://docs.angularjs.org/api/ng/service/$http https://docs.angularjs.org/api/ng/service/$http

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM