Reputation: 67
I'm trying to check if a user exist when clicking on Login button migrating from Spring Forms + JSTL + Spring MVC
to AngularJS + Spring MVC
, but I'm getting this error:
Failed to load resource: the server responded with a status of 415 (Tipo de Medio No Soportado)
I'm new to AngularJS and Spring so be nice.
This is my Java Controller:
@Controller
@SessionAttributes("empleado")
public class LoginController {
@Autowired
private IServiceEmpleado serviceEmpleado;
@RequestMapping(value="/login/check", method = RequestMethod.POST,consumes = {"application/json;charset=UTF-8"}, produces={"application/json;charset=UTF-8"})
public @ResponseBody void login(@RequestBody LoginDTO login, HttpSession session) throws ServicioException{
Empleado empleado = serviceEmpleado.correctLogin(login.getDni(), login.getPassword());
session.setAttribute("empleado",
empleado);
}
}
This is my AngularJS controller:
angular.module('electronicaDonPepe')
.controller('LoginController', ['$scope', '$http', '$state', '$stateParams',
function($scope, $http, $state, $stateParams) {
$scope.login = function(){
var param = {
dni: $scope.usuario.dni,
contrasenia: $scope.usuario.contrasenia
};
$http.post('http://localhost:8585/electronicaDonPepe/login/check', param).then(function(){
$state.go('login');
});
};
}]);
Finally, this is the error:
Request URL:http://localhost:8585/electronicaDonPepe/login/check
Request Method:POST
Status Code:415 Tipo de Medio No Soportado
Remote Address:[::1]:8585
Response Headers
view source
Cache-Control:no-cache
Cache-Control:no-store
Content-Language:es
Content-Length:1120
Content-Type:text/html;charset=utf-8
Date:Thu, 19 Jan 2017 13:44:02 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Pragma:no-cache
Server:Apache-Coyote/1.1
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:es-ES,es;q=0.8
Connection:keep-alive
Content-Length:43
Content-Type:application/json;charset=UTF-8
Cookie:JSESSIONID=4E1E018490CC15F29FBE205132C2CACD; __ngDebug=true
Host:localhost:8585
Origin:http://localhost:8585
Referer:http://localhost:8585/electronicaDonPepe/
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Request Payload
view source
{dni: "34631248", contrasenia: "34631248"}
contrasenia
:
"34631248"
dni
:
"34631248"
And some AngularJS Batarang hints:
Module "ui.router.util" was created but never loaded.
Module "ui.router.router" was created but never loaded.
Module "ui.router.state" was created but never loaded.
Module "ui.router" was created but never loaded.
Module "ui.router.compat" was created but never loaded.
Module "electronicaDonPepe" was created but never loaded.
I did my research but it seems to be a very particular case in each example I read. Thanks in advance.
Upvotes: 0
Views: 1495
Reputation: 2598
{dni: "34631248", contrasenia: "34631248"}
Here is your Error. You are passing an invalid JSON format type. Try to use any formater online and you will see that it will give you and error.
{"dni": "34631248", "contrasenia": "34631248"}
This is the correct way of a Json Format Type. Hope it help you. Now the two are passing in strings.
var param = {
"dni": $scope.usuario.dni,
"contrasenia": $scope.usuario.contrasenia
};
Then try with this.
Upvotes: 1
Reputation: 344
Try to set request header
$http.post('http://localhost:8585/electronicaDonPepe/login/check', param,{
headers: {
'Content-Type': 'application/json'
}
}).then(function(){
$state.go('login');
});
Upvotes: 0