Javascript decodeURI(Component) malformed uri exception

后端 未结 2 1555
遥遥无期
遥遥无期 2021-02-02 07:03

I entered the following in Chrome\'s console:

decodeURIComponent(\'a%AFc\');

Instead of resulting to a0xAFc, it caused a URIError

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-02 07:12

    This may or may not apply to someone else's situation but this is what did it for me so I thought I would share. I upload and download lots of text files to a custom CMS.
    the '%' sign in the source code was wreaking havoc for me.

    // send to server
    content = content.toString().replace(/%/g,'~~pct~~');       // ~~pct~~ <-made up replacement
    content = encodeURI(content);
    
    // get back from server / database
    content = decodeURI(content);
    content = content.toString().replace(/~~pct~~/g,'%');    // globally restore '%'
    

提交回复
热议问题