0

I'm using javascript and nodeJS for a page I'm building

I store cookies using this line in my JS file:

document.cookie = cname + "=" + cvalue + ";" + expires;

The value is mostly in Hebrew characters.

The cookie is stored correctly and is readable when I check it on the browser. Then I fetch the data to display it on the next page, and the Hebrew characters are not readable.

When I check the req. headers using:

router.get('/order', function(req, res){
console.log(req.headers.cookie);

I see that all the Hebrew characters turned to something like ×§××§×ס ×שק×××

How can I display them correctly on the HTML page?

note that in the head tag of my html file I use:

    <meta charset="UTF-8">
    <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"/> 
ishchai
  • 26
  • 3
  • To be able to use unicode characters in a cookie, you should use an encoding scheme. encodeURIComponent() or querystring.escape() in node when setting the cookie and decodeURIComponent() in the client should work. https://stackoverflow.com/questions/22872405/expressjs-unicode-cookie – QuentinUK Nov 21 '20 at 18:39
  • Wow Quentin! Thank you so much! I've been struggling with this for two days now, encodeURIComponent() did the trick. What's weird is that I didn't have to use decodeURIComponent() on the other side. It actually didn't work with it so I removed it and then it worked. – ishchai Nov 22 '20 at 16:46

0 Answers0