【问题标题】:Javascript + Spring bootJavascript + 春季启动
【发布时间】:2016-07-22 06:43:08
【问题描述】:

我正在尝试在 Spring Boot 中使用 webService 我有我的 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);
        }
    }

为了联系这个网络服务,我在 Spring Boot Server 的其他页面中使用 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 jquery spring-mvc spring-boot


【解决方案1】:

所以您正尝试从 JavaScript 向不同的主机或端口进行 API 调用? 由于 Same-Origin-Policy,默认情况下不允许这样做。但是,您可以在 Spring Boot Enable CORS in Spring Boot 中启用跨源请求

【讨论】:

  • 我正在测试这个谢谢
  • 但是我必须把我的html页面放在我的spring服务器上?
  • 不,本文的重点是全局激活CORS或为特定的Controller激活,这样您就不需要在同一个Spring Boot应用程序中拥有您的HTML + JavaScript。他们使用 Controller,但本质上它与您的 RestController 相同,因为它们也提供 JSON。
【解决方案2】:

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

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

如果您可以控制其他服务并且它是 Spring Boot 应用程序,那么您可以仅针对其他服务实施链接中建议的内容。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 2015-04-18
      • 2017-06-24
      • 2015-08-22
      • 2015-09-18
      • 2015-03-20
      • 2018-01-24
      • 2016-08-22
      • 1970-01-01
      相关资源
      最近更新 更多