kk19 kk19 - 24 days ago 8
AngularJS Question

$http POST angular-Submit constant public key at the time of login

I need to submit public key with constant key value, along with username and password at the time of login.And also to redirect it some other page in ionic app- href="#/tab/abc", if login is successful. Is this a right way:

Controller.js

.controller('LoginCtrl', function($scope, $state,$http, $rootScope,$window,$location) {
$scope.formData = {};
$scope.processForm = function() {
$http({
method : 'POST',
url : 'api/login ',
data : $.param($scope.formData),
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
})
.success(function (response) {

$scope.data=response;
$state.go('tab.abc');
})
.error(function (data, status, header, config) {
$window.alert("username or password incorrect");
});
};

})


How to submit this public key" X-API-KEY=eroo9rwabcor-ltjabcerabc9r" along with username and password at the time of login. And then redirect it to some other page in ionic app.

html

<form action="" class="ki-login-form" method="post" accept-charset="utf-8" >
<div class="form-group username">
<input type="text" class="form-control" name="username" ng-model="formData.username" value="" id="identity" placeholder="Your Name">
</div>
<div class="form-group pin">
<input type="text" class="form-control" name="password" ng-model="formData.password" value="" id="identity" placeholder="Your Pin">
</div>
<a type="submit" class="btns" ng-click="processForm(formData.username,formData.password)">Login</a>
</form>

Answer

I think you have to pass it through headers.

$http({
    method  : 'POST',
    url     : 'api/login ',
    data    : $.param($scope.formData),  
    headers : { 'Content-Type': 'application/x-www-form-urlencoded', 'X-API-KEY': 'eroo9rwabcor-ltjabcerabc9r' }  
})
.success(function (response) {
    $scope.data=response;
    $location.path('#/tab/abc');
})
.error(function (data, status, header, config) {
    $window.alert("username or password incorrect");
});