encodeURIComponent引发错误"URI格式错误" [英] decodeURIComponent throwing an error 'URI malformed'

查看:1069
本文介绍了encodeURIComponent引发错误"URI格式错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于不推荐使用unescape,所以我选择了decodeURIComponent,但是它没有按预期工作. decodeURIComponent无法解码以下URI组件

As unescape has been deprecated I have chosen decodeURIComponent , but it doesn't work as expected . decodeURIComponent cant decode the following URI component

Coast%20Guard%20Academy%20to%20hold%20annual%20Women%92s%20%91Leadhership%92%20event

在解码以上字符串的解码URIComponent时,会抛出错误,从而阻止了其余的javascript执行.

While decoding the above string decodeURIComponent throws an error, which blocks the remaining javascript execution.

是否有解决此问题的解决方案?

Is there is any solution to fix this ?

推荐答案

%91%92字符是使用ANSI代码页编码的. decodeURIComponent() 期望该字符串已编码为UTF-8 :

The %91 and %92 characters were encoded using an ANSI codepage. decodeURIComponent() expects the string to have been encoded as UTF-8:

decodeURIComponent函数计算URI的新版本,其中encodeURIComponent函数可能引入的每种转义序列和UTF-8编码类型的编码都将替换为其表示的字符.

The decodeURIComponent function computes a new version of a URI in which each escape sequence and UTF-8 encoding of the sort that might be introduced by the encodeURIComponent function is replaced with the character that it represents.

两个引号应编码为%E2%80%98%E2%80%99.

这篇关于encodeURIComponent引发错误"URI格式错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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