2016-09-22 53 views
-2

我遇到的一些问题,我不能能够使用PhoneGap的建立这是使用angularjs一个应用程序发送的HTTP POST请求到服务器,即时通讯,可我知道如何使用angularjs来做一个post请求?我的代码是这样的AngularJS HTTP POST错误:意外的要求:POST

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

+3

您将数据发送到您的POST请求的方式是不对的,你应该写数据 :{电子邮件: “EMAILADDRESS”},而不是数据:电子邮件= “电子邮件” –

+2

在'警报(误差)''error'没有定义。 .error与alert中的错误不同。 – varit05

+0

这是单元测试吗?如果没有,你应该**不**加载'angular-mocks.js'。如果是在一个测试,你将需要建立在给回调函数没关系'$ httpBackend' – Phil

回答

0

@ user3273700和@ varit05是完全正确的。此外,你有一个回调到AuthService.login()TestController不应该被这样定义的。你肯定错了应该如何使用控制器。回调登录方法应该是一个没有参数的普通函数。

更改此:

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

这样:

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

名字预期POST请求。你没有把'$ scope'作为参数加入是正确的,但我会假设OP应该传入* something *,例如promise的解析值 – Phil