2

I have a website, backend is in spring, frontend is in React. I save a cookie on backend using

Cookie cookie = new Cookie("userId", testDTO.getId());
userCookie.setPath("/");
response.addCookie(userCookie);

But when react posts request on the controller, my browser doesnt get that cookie. Storage in firefox is just empty.

Frontend code:

const requestOptions = {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({
                name: username,
                stayLogged: false
            })
        };

       fetch('http://localhost:8080/health-check/post',requestOptions)
           .then(response => response.json())
           .then(data =>{console.log(data);})
Thomas
  • 169
  • 10
  • https://stackoverflow.com/questions/22432616/why-is-the-browser-not-setting-cookies-after-an-ajax-request-returns have a look at – Deepak Uniyal May 14 '20 at 16:48

1 Answers1

0

I fixed it. Just added to the post mapping in the controller @CrossOrigin(origins = "http://localhost:3000", allowCredentials = "true")

Thomas
  • 169
  • 10