【问题标题】:Error when firing post request触发发布请求时出错
【发布时间】:2017-08-06 07:41:47
【问题描述】:

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

.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  

【问题讨论】:

  • 您遇到的错误是什么?您可以在错误回调函数中记录错误
  • 在不知道错误是什么的情况下不可能知道您的问题是什么,但是这种类型的代码可能遇到的最常见问题之一是 CORS。也许这会有所帮助? stackoverflow.com/questions/17756550/angularjs-cors-issues
  • 你试过像JSON.stringify($scope.user)这样发送你的数据吗?
  • 你必须让我们知道错误是什么。没有它就很难提供帮助
  • 我真的很抱歉!我刚刚更新了错误的问题。 @Lotus91 我试过没用。

标签: angularjs


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-03-02
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    相关资源
    最近更新 更多