4

I am trying to get a cookie in my client side javascript by calling 'document.cookie' but it returns ''. I know a cookie exists because I passed 'Set-Cookie' in my response from django's rest framework and the browser's dev tools show that a cookie exists.

dev tools shows cookie 'userID'

but again, when running 'document.cookie' in dev tools console window, nothing is returned:

enter image description here

The cookie doesn't have 'HttpOnly' flag set. I'm not sure how else to get existing cookies using javascript.

Randy Tang
  • 103
  • 1
  • 8
  • 2
    Hey, the html page is being rendered by the server or is it a local html file? – Lakshaya U. Mar 05 '22 at 03:49
  • 1
    @LakshayaU. the image says `127.0.0.1:8000` – Bravo Mar 05 '22 at 04:14
  • @Bravo Oh yeah it does – Lakshaya U. Mar 05 '22 at 04:17
  • LOL. thanks guys. i just came across the buried SO post about this problem. Apparently, the cookie's 'domain' must explicitly be OMITTED when working in a localhost environment. https://stackoverflow.com/questions/1134290/cookies-on-localhost-with-explicit-domain/1188145#1188145 what's strange is, the answer states not even an empty string should be set on it but i did that in my backend and it worked. – Randy Tang Mar 05 '22 at 04:40

0 Answers0