繁体   English   中英

Javascript + Spring启动

[英]Javascript + Spring boot

我正在尝试将WebService与Spring Boot一起使用,我有我的RestControllerPage:

@RequestMapping(
            value="/api/user/connection",
            method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces=MediaType.APPLICATION_JSON_VALUE
    )
    public ResponseEntity<User> connect(@RequestBody Authentification authentification){
        try {
            if (authentification.getLogins() == "" || authentification.getLogins() == null) {
                return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
            }
            String val = Md5.md5(authentification.getPwd());
            int nbuser = userDao.countUser(authentification.getLogins(), val);
            // we get the user for update the token
            LinkedList<User> listUser = userDao.findByLoginsAndPwd(authentification.getLogins(), val);
            if (listUser.get(0).getToken() == "" || listUser.get(0).getToken() == null) {
                listUser.get(0).setToken(Md5.md5(listUser.get(0).getId() + listUser.get(0).getLogins()));
            }
            if (userDao.save(listUser.get(0)) == null) {
                return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
            }
            return new ResponseEntity<User>(listUser.get(0),HttpStatus.OK);
        }catch(Exception e){
            return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

对于联系此Web服务我在Spring启动服务器的其他页面中使用Jquery:

<html>

<head>

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $('#BtnValidate').click(function(){
                $.ajax({
                    type: "POST",
                    contentType: 'application/json',
                    dataType:'json',
                    url:'http://localhost:9000/api/user/connection',
                    data:JSON.stringify({
                        logins:$('#Txtlogin').val(),
                        pwd:$('#TxtPwd').val()
                    }),
                    success: function(data){
                        $('.notification').html("<p> response : "+data.d+"</p>");
                    },
                    error:function(){
                        alert('Error in the webService');
                    }

                });
                return false;
            });


        });

    </script>
</head>

<body>

        <label>Login : </label>
        <input type="text" id="Txtlogin">
        <br/>
        <label>Pwd : </label>
        <input type="pwd" id="TxtPwd">
        <br/>
        <input type="button" id="BtnValidate" value="validate"/>

    <div class="notification">

    </div>
</body>
</html>

我得到这个错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.

我真的很锁定。

谢谢

因此,您正在尝试从JavaScript到其他主机或端口进行API调用吗? 由于Same-Origin-Policy,默认情况下不允许这样做。 但是,您可以在Spring Boot中启用 Spring Boot Enable CORS中的 Cross Origin Requests

必须将CORS标头添加到您的请求的目标。 因此,如果Spring Boot应用程序在其上下文之外调用服务,则需要向该服务添加CORS支持。 执行此操作的方式取决于运行服务的平台。

例如,在AWS上,您可以在设置中为S3网站设置CORS支持。

如果您可以控制其他服务并且它是Spring Boot应用程序,那么您可以在链接中仅为其他服务实现建议。

您可以使用@crossorigin批注接受所有请求标头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM