I have a very simple line of code that set and read a cookie. I kept getting empty value for my cookie and have no understanding why. I have cookie enabled and know that cookies work on the browser.
<HTML> <HEAD> <TITLE>Hello World</TITLE> </HEAD> <BODY> <SCRIPT type="text/javascript"> document.cookie = "ding=dong"; </SCRIPT> <script type="text/javascript"> alert(document.cookie); </script> </BODY> </HTML>
Create a Cookie with JavaScriptJavaScript can create, read, and delete cookies with the document. cookie property. With JavaScript, a cookie can be created like this: document.
Check out the OPTIONS response header ACCESS-CONTROL-ALLOW-CREDENTIAL whether it is set to true . If the server doesn't allow credentials being sent along, the browser will just not attach cookies and authorization headers. So this could be another reason why the cookies are missing in the POST cross-site request.
Cookies are usually set by a web-server using the response Set-Cookie HTTP-header. Then, the browser automatically adds them to (almost) every request to the same domain using the Cookie HTTP-header.
At the top right corner, click Settings and more , then choose Settings. Select Cookies and site permissions. Click Manage and delete cookies and site data, then turn ON Allow sites to save and read cookie data (recommended). Select JavaScript under Site permissions, then turn ON Allowed (recommended).
Recently I stumbled upon a similar issue. My script couldn't store a cookie in Chromium, although it worked well on all other major browsers. After some googling it turned out that Chrome ignores cookies from local pages. After I uploaded the page to remote server code magically started to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With