AngularJS - 使用$ http.POST提交表单时响应时间极短

时间:2017-03-20 19:47:47

标签: javascript angularjs node.js forms post

我对Angular的经验很少,现在我在使用ng-submit()提交基本HTML表单后发送POST请求时遇到问题。后请求转到Node.js服务器,使用http服务时响应时间非常慢。必须有问题的代码在scope.submitMeeting() - 函数中。它从meetingService(addMeeting)中触发一个方法。

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

app.service('meetingService', function($http) {
  this.getNewData = function(date) {
    return $http({
    method: 'GET',
    url: '/meetings/date/' + date
    })
  }
  this.addMeeting = function(formData) {
    return $http({
      method: 'POST',
      data: formData,
      url: '/meetings'
    })
  }
});

app.controller('adminCtrl', function ($scope, $http, $path, meetingService){
  $http({
    method: 'GET',
    url: '/meetings/date/' + getToday()
  }).then(function successCallback(response) {
    $scope.meetings = response.data;
  }, function errorCallback(error) {
    alert(error.statusText);
  });

  var formData = [];

  $scope.submitMeeting = function(formData) {
    formdata = $scope.form;
    meetingService.addMeeting(formdata).then(function successCallback(response) {
      alert('You have added a meeting!');
    }, function errorCallback(error) {
      alert(error.statusText + ': Something went terribly wrong!');
    });
  }

  $scope.dateSearch = getToday();
  $scope.submitSearch = function() {
    meetingService.getNewData($scope.dateSearch).then(function successCallback(response) {
      $scope.meetings = response.data;
    }, function errorCallback(error) {
      alert(error.statusText);
    });
  }
});

据我所知,这里没有错。在使用Postman进行测试时,我的后端似乎也正常工作。以下是参考表格:

<form ng-submit="submitMeeting()">
          <div class="form-group">
            <label for="exampleInputEmail1">Dato</label>
            <input type="text" class="form-control" id="datepicker2" aria-describedby="emailHelp" placeholder="Velg dato" name="date" ng-model="form.date" required>
          </div>
          <div class="form-group">
            <label for="exampleInputEmail1">Klokkeslett</label>
            <div id="timeSelect">
              <select name="time" ng-model="form.time" required>
                <option>08</option>
                <option>09</option>
                <option>10</option>
                <option>11</option>
                <option>12</option>
                <option>13</option>
                <option>14</option>
                <option>15</option>
                <option>16</option>
                <option>17</option>
                <option>18</option>
                <option>19</option>
                <option>20</option>
                <option>21</option>
                <option>22</option>
                <option>23</option>
                <option>00</option>
                <option>01</option>
                <option>02</option>
                <option>03</option>
                <option>04</option>
                <option>05</option>
                <option>06</option>
                <option>07</option>
              </select>
            </div>
          </div>
          <div class="form-group">
            <label for="exampleInputPassword1">Firma</label>
            <input type="text" class="form-control" placeholder="Skriv inn firma" name="company" ng-model="form.company" required>
          </div>
          <div class="form-group">
            <label for="exampleInputPassword1">Gjest</label>
            <input type="text" class="form-control" placeholder="Skriv inn gjest" ng-model="form.guest" name="guest">
          </div>
          <div class="form-group">
            <label for="exampleSelect2">Møterom</label>
            <input type="text" class="form-control" placeholder="Skriv inn møterom/sted" ng-model="form.room" name="room" required>
          </div>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Tilbake</button>
                <button type="submit" class="btn btn-primary">Legg til</button>
              </div>
            </div>
          </form>

1 个答案:

答案 0 :(得分:0)

事实证明这是一个像许多人预测的服务器端错误。我错过了一个res.json() - 语句,因此没有响应从POST路由返回给客户端。