DistribuzioneGaussiana DistribuzioneGaussiana - 5 months ago 28
AngularJS Question

HTTP request with angularJS

I'm trying to make an HTTP request from AngularJS v1.5.8 but it does not work.

I have a simple HTML form and a submit button that calls my login() function:

<body ng-app="myApp" ng-controller="loginController">
......
......
......

<div
class="col-md-4 col-md-offset-4 col-sm-4 col-sm-offset-3 col-xs-6 col-xs-offset-3">
<button type="submit" class="btn btn-default" ng-submit="login()">Submit</button>
</div>


And this is my
loginController


var app = angular.module ("myApp", []);
app.controller("loginController", function($scope, $http){

$scope.username = "";
$scope.password = "";

$scope.login = function() {

$http(
{
method : 'POST',
url : SessionService.getserverName()+'/RestServices/services/login/add',
headers : {
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
},
data : {
username : $scope.username,
password : $scope.password
}
}).success(function(response, status) {
if (response.result.success && status == 200) {
$log.info('OK');

$location.path('/newPage.html');
}
})
}
});


The HTTP request does not really run.

Answer

ngSubmit attribute work only on form element. see https://docs.angularjs.org/api/ng/directive/ngSubmit.

Try to move

ng-submit="login()"

to your form element

Comments