ExpressJS unicode cookie

瘦欲@ 提交于 2021-02-10 18:32:24

问题


Is there any way to send unicode values in a cookie in expressjs. When a user is using a Spanish locale, a cookie is serialized with the user name, which can be something like "José"

e.g:

 res.cookie('user', JSON.stringify({name: 'José'}));

However in the client I get a cookie with the value:

{name: 'José'}

is there any way around this? The server already outputs that.


回答1:


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.

It's not specific to express, but how cookies work in general. See this answer for details: Allowed characters in cookies



来源:https://stackoverflow.com/questions/22872405/expressjs-unicode-cookie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!