[英]Error 415 when trying to consume $http.post service from AngularJS to Spring MVC - Maven
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: 我正在尝试单击从Spring Forms + JSTL + Spring MVC
迁移到AngularJS + Spring MVC
Login按钮时,是否存在用户,但出现此错误:
Failed to load resource: the server responded with a status of 415 (Tipo de Medio No Soportado) 无法加载资源:服务器以状态415响应(Tipo de Medio No Soportado)
I'm new to AngularJS and Spring so be nice. 我是AngularJS和Spring的新手,所以很好。
This is my Java Controller: 这是我的Java控制器:
@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: 这是我的AngularJS控制器:
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: 还有一些AngularJS Batarang提示:
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. 提前致谢。
{dni: "34631248", contrasenia: "34631248"}
Here is your Error. 这是您的错误。 You are passing an invalid JSON format type. 您正在传递无效的JSON格式类型。 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. 这是Json格式类型的正确方法。 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. 然后尝试这个。
Try to set request header 尝试设置请求标头
$http.post('http://localhost:8585/electronicaDonPepe/login/check', param,{
headers: {
'Content-Type': 'application/json'
}
}).then(function(){
$state.go('login');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.