简体   繁体   中英

AngularJS Http Post Error: Unexpected request: POST

i encounter some issues that i cant able to send the http post request to the server, im using phonegap to build a application which is using angularjs, may i know how to use angularjs to do a post request? And my code is like this

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

After i ran this i got some error like

 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)

@user3273700 and @varit05 are totally right. Also, the TestController that you have as a callback to the AuthService.login() should NOT be defined this way. You are definitely mistaken about how controllers should be used. The callback to login method should be a normal function with no parameters.

Change this:

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

to this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM