AngularJS Http Post Error:意外请求:POST

时间:2016-09-22 02:47:21

标签: javascript angularjs cordova ionic-framework

我遇到一些问题,我无法将http发送请求发送到服务器,我使用phonegap来构建一个使用angularjs的应用程序,我可能知道如何使用angularjs来发布请求吗?我的代码就像这样



 .controller('loginCtrl', function($scope, $http , $state, $ionicPopup, AuthService) {
    $scope.login = function(data) {
      AuthService.login(data.username, data.password).then(function TestController($scope) {
        $scope.username = $http({
          url: 'http://samedomain.com/GetPersons',
          method: "POST",
          data: email="email",
          headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function (data, status, headers, config) {
          $scope.data = data; // how do pass this to $scope.persons?
        }).error(function (data, status, headers, config) {
          alert(error);
          $scope.status = status;
        });

      });
    };




我跑了之后我得到了一些错误,比如



Error: Unexpected request: POST http://192.168.30.196:8086/user/login
No more request expected
    at $httpBackend (http://192.168.30.60:3000/lib/angular-mocks/angular-mocks.js:1418:9)
    at sendReq (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:24793:9)
    at serverRequest (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:24503:16)
    at processQueue (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:29127:28)
    at http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:29143:27
    at Scope.$eval (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:30395:28)
    at Scope.$digest (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:30211:31)
    at Scope.$apply (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:30503:24)
    at HTMLButtonElement.<anonymous> (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:65426:13)
    at defaultHandlerWrapper (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:16787:11)
 ReferenceError: error is not defined
    at http://192.168.30.60:3000/js/controllers.js:82:17
    at http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:24540:13
    at processQueue (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:29127:28)
    at http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:29143:27
    at Scope.$eval (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:30395:28)
    at Scope.$digest (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:30211:31)
    at Scope.$apply (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:30503:24)
    at HTMLButtonElement.<anonymous> (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:65426:13)
    at defaultHandlerWrapper (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:16787:11)
    at HTMLButtonElement.eventHandler (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:16775:9)
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

@ user3273700和@ varit05是完全正确的。此外,不应以这种方式定义作为TestController回调的AuthService.login()。你肯定错误地知道应该如何使用控制器。 login方法的回调应该是没有参数的普通函数。

改变这个:

AuthService.login(data.username, data.password).then(function TestController($scope) {

到此:

AuthService.login(data.username, data.password).then(function () {