我在点击登录按钮从Spring Forms + JSTL + Spring MVC
迁移到AngularJS + Spring MVC
时尝试检查用户是否存在,但我收到此错误:
无法加载资源:服务器响应状态为415(Tipo de Medio No Soportado)
我是AngularJS和Spring的新手,所以很好。
这是我的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);
}
}
这是我的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');
});
};
}]);
最后,这是错误:
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"
一些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.
我做了我的研究,但在我阅读的每个例子中似乎都是一个非常特殊的情况。提前谢谢。
答案 0 :(得分:1)
{dni: "34631248", contrasenia: "34631248"}
这是你的错误。您传递的是无效的JSON格式类型。尝试在线使用任何形成器,你会发现它会给你和错误。
{"dni": "34631248", "contrasenia": "34631248"}
这是Json格式类型的正确方法。希望它对你有所帮助。现在两人正在传递字符串。
var param = {
"dni": $scope.usuario.dni,
"contrasenia": $scope.usuario.contrasenia
};
然后试试这个。
答案 1 :(得分:0)
尝试设置请求标头
$http.post('http://localhost:8585/electronicaDonPepe/login/check', param,{
headers: {
'Content-Type': 'application/json'
}
}).then(function(){
$state.go('login');
});