Unserialize form values

I used serialize() to get the values ​​of the form, to get the values ​​back, I split the serialized string, but the values ​​are encoded using uri, for example, '@' is replaced with "% 40", I used decodeURIComponent() to decode, the problems looked like resolved, but still I replace the spaces with a +. can use string.replace() , but it will replace my legal "+" signs in a string. How to reach it?

+4
source share
1 answer

If there is a legal + in the string, it will already be encoded as %2B . Therefore, before calling decodeURIComponent() in a string, replace all + that represent a space in the string by space, and then call decodeURIComponent() to decode the string.

Use this code

 var str = "%4Bseri%2Balized+String+plus" str = str.replace(/\+/g, " "); str = decodeURIComponent(str); alert(str); 

Demo

+3
source

Source: https://habr.com/ru/post/1393476/


All Articles