C
C
copal2015-12-23 19:15:11
Laravel
copal, 2015-12-23 19:15:11

Why are cookies not being sent to the server?

The local server is available at 127.0.0.1:80 === api.domain.ru which has two routes api/login and api/authenticate.
The first route returns -

return response($user->toJSON())
->withCookie(cookie(
                        'token',
                        compact('token'),
                        500000,
                        '/',
                        '127.0.0.1:80',
                        false,
                        false
                    ));

On the client -
<button class="send-button">send</button>
<button class="authenticate">authenticate</button>
<script>
  $('.send-button').click(function(){
      $.ajax({
          url: 'http://127.0.0.1:80/api/login',
          method: 'POST',
          data: {
              email: '[email protected]',
              password: 'secret',
              XDEBUG_SESSION_START: 10156
          }
      });
  });

  $('.authenticate').click(function(){
      $.ajax({
          url: 'http://127.0.0.1:80/api/authenticate',
          method: 'GET',
          data: {
              XDEBUG_SESSION_START: 10156
          }
      });
  });
</script>

I press the first button and in the browser I find confirmation that the cookie is set. Then I press the second button and the breakpoint is triggered along the second path. I'm looking for cookies, there are no cookies. Why isn't the cookie coming to the server?
The client is at localhost:8080...
f50b8b0eb6ac449eb788c2832f84b060.png
3575bc1456e64862b9732c279de0a74f.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis, 2015-12-24
@copal

You don't need to look at cookies, but headers, because this is CORS. (ports are different)
The server should return
Access-Control-Allow-Credentials
and some other headers.
(the header "conflicts" with origin, more details in the RFC, so that in a panic you don’t understand why nothing works)
And with ajax (jQuery) api.jquery.com/jQuery.ajax
use withCredentials

$.ajax({
   url: a_cross_domain_url,
   xhrFields: {
      withCredentials: true
   }
});

R
romy4, 2015-12-23
@romy4

> http://127.0.0.1:80
> localhost:8080
something unclear connection

M
Miku Hatsune, 2015-12-23
@Hatsune-Miku

Maybe I'm wrong, but you set a cookie on the domain 127.0.0.1:80, and judging by the comments, your API is 127.0.0.1:8080.
I don’t know if the port affects the “equality” of addresses, but ... I think you should check

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question