触发发布请求时出错

时间:2017-03-15 13:58:59

标签: angularjs

当我使用邮递员提交注册表单时,它工作正常,但是当我尝试使用以下代码提交时,它显示出一些错误,我无法弄清楚,有人请帮帮我吗?

.controller('RegisterCtrl', function($scope, AuthService, $state, $http) {
  $scope.user = {
    name: '', 
    mobile:'',
    email:'',
    password:''
  };

  $scope.signup = function() {
    $http.post("http://localhost:8080/api/signup", $scope.user, {headers: {'Content-Type': 'application/json'} })
        .then(function (response) {
            return response;
        });
  };
})

当我检查Chrome浏览器时,它会记录以下错误:

angular.js:14362 Error: Unexpected request: POST http://localhost:8080/api/signup
No more request expected
    at $httpBackend (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-mocks.js:1402:9)
    at sendReq (http://localhost:3000/bower_components/angular/angular.js:12178:9)
    at serverRequest (http://localhost:3000/bower_components/angular/angular.js:11930:16)
    at processQueue (http://localhost:3000/bower_components/angular/angular.js:16689:37)
    at http://localhost:3000/bower_components/angular/angular.js:16733:27
    at Scope.$eval (http://localhost:3000/bower_components/angular/angular.js:18017:28)
    at Scope.$digest (http://localhost:3000/bower_components/angular/angular.js:17827:31)
    at ChildScope.$apply (http://localhost:3000/bower_components/angular/angular.js:18125:24)
    at HTMLInputElement.<anonymous> (http://localhost:3000/bower_components/angular/angular.js:26813:23)
    at HTMLInputElement.dispatch (http://localhost:3000/bower_components/jquery/dist/jquery.js:4435:9) Possibly unhandled rejection: {}(anonymous function) @ angular.js:14362(anonymous function) @ angular.js:10859processChecks @ angular.js:16715$eval @ angular.js:18017$digest @ angular.js:17827$apply @ angular.js:18125(anonymous function) @ angular.js:26813dispatch @ jquery.js:4435elemData.handle @ jquery.js:4121  

1 个答案:

答案 0 :(得分:0)

正如已经指出的那样,您的描述中缺少许多重要细节。我最好的猜测是,您的端点期望您的用户对象被命名为“user”,在这种情况下,您的帖子应该看起来更像以下内容:

$http.post(
    "http://localhost:8080/api/signup",
    { user: $scope.user }, 
);