我有一个Web应用程序,首先将JSON数据存储在cookie中,然后每隔x秒将其保存到数据库中.它只是打开与服务器的连接,服务器读取cookie.它实际上并不通过POST或GET发送任何内容.
当我保存到cookie时,我的数据格式良好.但是,当我在PHP中使用它,然后setcookie一个新的json_encoded数组时,它用符号替换了空格,然后这些出现在我的Web应用程序中.我找不到禁用json_encode字符串编码的任何方法,也没有找到解析那些加号的JS方法(使用jQuery.parseJSON; stringify的解析也不起作用)…没有人知道:S吗?
解决方法:
Note that the value portion of the cookie will automatically be urlencoded when you send the cookie, and when it is received, it is automatically decoded and assigned to a variable by the same name as the cookie name. If you don’t want this, you can use
setrawcookie()
instead if you are using PHP 5.
但我认为您仍然希望对Cookie URL进行编码,只希望使用空格而不是.但是,urlencode
:
[…] for historical reasons, spaces are encoded as plus (+) signs
您可以尝试使用rawurlencode
自己对其进行编码:
Returns a string in which all non-alphanumeric characters except
-_.~
have been replaced with a percent (%) sign followed by two hex digits. This is the encoding described in RFC 3986 […]
然后setrawcookie设置cookie.不幸的是,decodeURI
、decodeURIComponent
甚至不推荐使用的unescape
JavaScript函数都不会将back转换为空格.因此,您可能会陷入困境,迫使每个人都难以理解.