简体   繁体   中英

Cookie not getting sent on first request

I am trying to understand cookies and what causes them to be sent as part of a request. My basic understanding of cookies is that they are name value pairs that get sent with every request based on the path variable. I am doing a basic test that seems to suggest that cookies are not sent on an initial request. My test is this.

  1. Open up a chrome incognito mode.
  2. Press F12, enter document.cookie = "test_value=test_name;path=/;" into the console.
  3. Go to google.com.

Now if I look at the network tab and look at the request to google.com. Under the cookies tab I can see that no cookies were sent with the request. Why not?

在此处输入图片说明

Furthermore, once I am at www.google.com, if repeat steps 2 and 3 in the above procedure, the cookie does get sent this time. Not only does it get sent, I get 4 response cookies.

在此处输入图片说明

So why does the cookie not get sent the first time, and why do I get 4 response cookies the second time?

The first page Google Chrome shows when user starts incognito mode is handled differently from the other ones. It doesn't share browser resources (like cookies storage, ...) with Internet pages. If you try with another page you'll see that it works correctly.

For example, if you go to www.example.com , type document.cookie = "testCookie=value; path=/" in JavaScript console and refresh the page you'll see the header Cookie containing it in HTML request.

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