简体   繁体   中英

Javascript + Spring boot

I am trying to use a webService with Spring Boot I have my 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);
        }
    }

And for contact This web service I am Using Jquery in other page out of the Spring boot Server :

<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>

And i get this error :

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.

I am really lock.

Thank you

So you are trying to make an API call from JavaScript to a different host or port? This is by default not permitted due to Same-Origin-Policy. You can however enable Cross Origin Requests in Spring Boot Enable CORS in Spring Boot

the The CORS header must be added to target of your request. So if the Spring Boot application invokes a service outside its context, you'll need to add CORS support to that service. The way you do this depends on the platform that runs your service.

On AWS for example, you can set CORS support for an S3 website in the settings.

If you have control over the other service and it's a Spring Boot application, then you can implement what's suggested in the link only for the other service.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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