代码之家  ›  专栏  ›  技术社区  ›  Nicoleta Wilskon

DecodeURIComponent不支持%uxxx编码的组件

  •  0
  • Nicoleta Wilskon  · 技术社区  · 7 年前

    DecodeURIComponent不支持少数编码组件

    我正在用json的公告格式发送JD,格式为restapi。所以我对jd进行编码并发送。这工作正常,没有任何问题。但当我试图解码编码的JD时,我得到了错误,因为URI格式不正确

    var jd = "Where are bullets most often used?
    
     - Technical writing
     - Reference works
     - Notes
     - Presentations";
    
    
    var json ={
    "job":encodeURIComponent(escape(jd));
    
    }
    

    解码:

    var jd = decodeURIComponent(jd);
    

    这是我收到的回复。

    Where%20are%20bullets%20most%20often%20used%3F%0A%uF0B7Technical%20writing%0A%uF0B7Sub%20bullet%0A%uF0B7Reference%20works%0A%uF0B7Notes%0A%uF0B7Presentations%0A%uF0B7Lists%0AAn%20alternative%20method%20is%20to%20use%20a%u807Dnumbered%20list%3A%0A1.Technical%20writing%0A2.Reference%20works%0A3.Notes%0A4.Presentations%0A5.Lists
    
    2 回复  |  直到 5 年前
        1
  •  1
  •   John Velasquez    7 年前

    你必须 unescape 首先

    var jd = decodeURIComponent(unescape(json.job));
    
        2
  •  0
  •   Community CDub    5 年前

    避免使用 escape() .

    从文件中:

    逃脱

    这个 函数计算一个新字符串,其中某些字符已被十六进制转义序列替换。

    描述

    转义函数是全局对象的一个属性。对特殊字符进行编码,但以下字符除外: @*_+-./

    字符的十六进制形式,其代码单位值为 0xFF 或更少,是一个两位数的转义序列: %xx .对于代码单位更大的字符,四位数格式 %uxxxx 被使用了。

    不赞成。不适用于新网站。

    — MDN JavaScript Reference - escape()

    维基百科:

    Unicode字符存在非标准编码: %Uxxx 哪里 xxxx 是用四个十六进制数字表示的UTF-16代码单元。该行为未由任何RFC指定,并且已经过验证 rejected 由W3C提供。ECMA-262的第三版仍然包括 escape 使用此语法的函数,以及 encodeURI encodeURIComponent 函数对字符串应用UTF-8编码,然后对结果字节进行百分比转义。

    — Wikipedia - Percent encoding - Non-standard implementations

    推荐文章