反序列化表单值
我使用 serialize()
来获取表单值,为了获取值,我分割了序列化字符串,但是这些值是 uri 编码的,例如“@”被替换为“%40”,我使用了 decodeURIComponent()
进行解码,问题看起来已经解决,但我仍然将空格替换为“+”号。可以使用 string.replace() 但它会替换字符串中合法的“+”符号。如何实现?
I used serialize()
to get form values, for getting values back i split the serialized string, but the values are uri encoded, like '@' is replaced by '%40', i used decodeURIComponent()
to decode, the issues looked like solved but still i am getting spaces replaced by '+' sign. can use string.replace()
but it would replace my legitimate '+' signs in the string. How to achieve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果字符串中存在合法的
+
,则它已经被编码为%2B
。因此,在对字符串调用decodeURIComponent()
之前,将字符串中代表空格的所有+
替换为空格,然后调用decodeURIComponent()
解码字符串。使用此代码
演示
If there are legitimate
+
in the string it will already be encoded as%2B
. So before calllingdecodeURIComponent()
on the string replace all the+
which represent the space in the string by space and then calldecodeURIComponent()
to decode the string.Use this code
Demo