Javascript decodeURI(Component) 格式错误的 uri 异常 [英] Javascript decodeURI(Component) malformed uri exception

查看:24
本文介绍了Javascript decodeURI(Component) 格式错误的 uri 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Chrome 的控制台中输入了以下内容:

I entered the following in Chrome's console:

decodeURIComponent('a%AFc');

它不是导致 a0xAFc,而是导致了 URIError 异常(URI 格式错误).

Instead of resulting to a0xAFc, it caused a URIError exception (malformed uri).

我听过很多理由为什么这可能是可能的,但我不明白的是为什么?

I've heard several excuses why this may be possible, but what I don't understand is why?

decodeURIComponent() 函数尤其应该解码数据,而不是验证 URI.

The decodeURIComponent() function in particular is supposed to decode data, not verify the URI.

推荐答案

%AF 本身不是一个字符,而是 Unicode 序列的一部分 (MACRON - %C2%AF).

%AF is not a character on his own but part of Unicode sequence (MACRON - %C2%AF).

%AF 不是由 encodeURIComponent 产生的,而是类似于 escape 的东西,所以它可以被 unescape 解码>.

%AF wasn't produced by encodeURIComponent but something like escape, so it can be decoded by unescape.

您可能需要的是 decodeURIComponent('%C2%AF')

这篇关于Javascript decodeURI(Component) 格式错误的 uri 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆