「已注销」 2014-02-01 13:03
浏览 715

如何在JavaScript中解码golang url.QueryEscape数据?

I have a string in JS side which is url.QueryEscaped.

Spaces were replaced with + sign by url.QueryEscape. They don't get converted back to space in decodeURIComponent. Should I manually do a string replace all + with space? What is the right way to decode it?

  • 写回答

1条回答 默认 最新

  • dongshao8471 2014-02-01 15:34
    关注

    One simple method is to replace all the + characters with spaces prior to decoding. For example:

    decodeURIComponent("%2f+%2b".replace(/\+/g, " "))
    

    will correctly decode the string to "/ +". Note that it is necessary to perform the replacement prior to decoding, since there could be encoded + characters in the string.

    评论

报告相同问题?