简体   繁体   English

尝试从AngularJS到Spring MVC使用$ http.post服务时出现错误415-Maven

[英]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.

相关问题 Java AngularJS Spring MVC - 将 bean 发送到控制器时,$http.post 400 错误请求在语法上不正确 - Java AngularJS Spring MVC - $http.post 400 bad request syntactically incorrect when sending bean to controller AngularJs $ http.post可以春季启动吗? - AngularJs $http.post to spring boot? 将带有JSON数据的POST请求从HTML发送到Java Rest Web服务时出现HTTP错误415 - HTTP Error 415 when sending POST request with JSON data from HTML to Java Rest web service 无法添加图片http.post POST 500内部服务器错误。 AngularJS Java Spring - Can't add Image http.post POST 500 internal server error. AngularJS Java Spring 错误415.使用Spring的Service REST-AngularJs - Error 415. Service REST with Spring - AngularJs 当我尝试使用JSON数据时,POST到Jersey REST服务获取错误415不支持的媒体类型 - POST to Jersey REST service getting error 415 Unsupported Media Type when I try to consume JSON data 与angularjs $ http.post和spring restful的错误请求 - bad request with angularjs $http.post and spring restful 发布表格时出现Spring MVC 415错误 - Spring MVC 415 error when posting form 在Spring MVC上执行POST请求时,HTTP状态415 –不支持的媒体类型 - HTTP Status 415 – Unsupported Media Type when doing POST request on Spring MVC 如何在使用 spring boot 的 REST Web 服务中的 POST 请求期间修复“HTTP-415”错误 - How to fix 'HTTP-415' error, during POST request in REST web service using spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM