Trying to send a post response to this method in spring mvc:
@RestController
public class LoginRController {
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login_Post(@RequestParam(value = "user", required = true) String username, @RequestParam(value = "pass", required = true) String password) {
return ("Login: "+username+", "+password);
}
}
So I initally tried:
Controller
App.controller('LoginController', [
'$scope',
'LoginService',
function($scope, LoginService) {
var self = this;
self.authUser = function() {
LoginService.authUser($('#inputEmail').val(),
$('#inputPassword').val()).then(function(d) {
console.log("This happened");
}, function(errResponse) {
console.error('Error while fetching Currencies');
});
};
} ]);
Service
App.factory('LoginService', [
'$http',
'$q',
function($http, $q) {
return {
authUser : function(username, password) {
return $http.post(
'login',
"user=" + encodeURIComponent(username) + "&pass="
+ encodeURIComponent(password)).then(
function(response) {
return response.data;
}, function(errResponse) {
console.error('Error @ authUser');
return $q.reject(errResponse);
})
}
}
} ]);
I found that it always returns a 400 Error which means the required fields dont match up... Which is odd because I can use Jquery $post() just fine.
Also I tried everything here on this thread, AngularJs $http.post() does not send data and How do I POST urlencoded form data with $http in AngularJS?
Also tried:
'use strict';
App.factory('LoginService', [ '$http', '$q', function($http, $q) {
return {
authUser : function(username, password) {
return $http.post('login', {
params : {
user : username,
pass : password
}
}, {
headers : {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) {
return response.data;
}, function(errResponse) {
console.error('Error @ authUser');
return $q.reject(errResponse);
})
}
}
} ]);