【问题标题】:NodeJS decodeURIComponent not working properlyNodeJS decodeURIComponent 无法正常工作
【发布时间】:2014-02-07 20:56:33
【问题描述】:

当我尝试使用 decodeURLCompnent 在 nodeJS 中解码下面的字符串时:

var decoded = decodeURI('Ulysses%20Guimar%C3%A3es%20-%20lado%20par');
console.log(decoded);

我明白了

Ulysses Guimarães - lado par

代替

Avenida Ulysses Guimarães - lado par 

但是当我在客户端(浏览器)上使用相同的代码时,我可以获得正确的字符 'ã'。

有没有办法在 Node 脚本中将 ã 转换为 ã

【问题讨论】:

    标签: node.js utf-8 character-encoding latin1 decodeuricomponent


    【解决方案1】:

    我无法在 0.10 或 0.11 版本的节点中重现它。

    您可以使用new Buffer('Ulysses Guimarães - lado par', 'binary').toString('utf8') 将第一个转换为第二个,但这是一种解决方法,而不是解决方案。

    你确定你调用的是 decodeURI,而不是 unescape?

    【讨论】:

      【解决方案2】:

      使用var querystring = require("querystring");

      querystring.unescape() 方法对给定 str 上的 URL 百分比编码字符进行解码。

      然后根据文档querystring.unescape(str)

      https://nodejs.org/api/querystring.html#querystring_querystring_unescape_str

      【讨论】:

        【解决方案3】:

        我只是把它留在这里,因为我遇到了同样的问题。我在客户端和 Nodejs 中使用 encodeURIcomponent(str) 函数时,decodeURI(str) 遇到了同样的问题。我在客户端使用encodeURI(str) 解决了这个问题。

        【讨论】:

        • 如果你使用 encodeURIcomponent,你应该使用“decodeURIComponent”而不是“decodeURI”。
        猜你喜欢
        • 2018-04-26
        • 1970-01-01
        • 2023-01-18
        • 2014-11-15
        • 2019-07-10
        • 2014-01-27
        • 2012-04-23
        • 1970-01-01
        • 2020-11-18
        相关资源
        最近更新 更多