Two way hashing JSON String in JavaScript for use in URL

后端 未结 1 623
醉梦人生
醉梦人生 2021-01-06 01:09

I would like to take a JSON string and encrypt/hash/encode it so that I can put it into a URL so that it would resemble something such as seen below:

var str         


        
相关标签:
1条回答
  • 2021-01-06 01:38

    Use encodeURIComponent() to encode it for the url

    To decode use the decodeURIComponent() function

    Base64 is not URL safe since it can contain non url characters like / + -. (See this question)

    If you want your url to not be too similair to the original string you can first covert to base64 and then encode and reverse by decoding and covrrtibg back from base 64

    // to url
    encodeURIComponent(btoa(str))
    
    // from url
    atob(decodeURIComponent(uri))
    
    0 讨论(0)
提交回复
热议问题