被解码的URI不是合法的编码的解决

疑问被确定在了那个大大的prototype.js文件上。出错的脚本行最终被定位到一句调用decodeURIComponent方法的地方,找到了就好办了,不想去改prototype.js,干脆写个自己的decodeURIComponent覆盖它。查了文档知道JScript 5.5 新增的以下4个跟URI编码解码相关的函数:
    encodeURI,decodeURI,encodeURIComponent,decodeURIComponent

测试发现,这里的4个函数(方法)和Java中的类似,都是以Unicode标准进行编码解码的,也就是说将汉字视为3字节进行处理,一个汉字编码后变为类似的 %hh%hh%hh 的形式,相应的,解码函数的参数也必须复合编码结果的格式,二者可逆,否则就会报错“被解码的 URI 不是合法的编码” 。而我们程序中比较常用的GB2312, GBK, UTF-8处理中文都是2字节的,这是导致问题的根源。例:在编码为GBK的ASP中,使用Server.URLEncode("中")的结果是%D6%D0,但在客户端js里encodeURI("中") encodeURIComponent("中")的结果是%E4%B8%AD ,而如果把%D6%D0传给decodeURIdecodeURIComponent ,就会报错“被解码的 URI 不是合法的编码” 。

不想改prototype.js的话,自己写函数覆盖上面4个方法的默认实现。下面是代码。供参考。

<script language="vbscript">
Function URLEncode(str)

Dim s,c,a,t

For i=1 To Len(str)

c = Mid(str,i,1)

a = ASC(c)

If a<0 OR a>255 Then

t=Hex(a)

While len(t) mod 2 > 0

t = "0" & t

Wend

For j=1 To len(t)-1 Step 2

s = s & "%" & Mid(t,j,2)

Next

ElseIf a=32 Then

s = s & "+"

ElseIf (a>=48 and a<=57) or (a>=65 and a<=90) or (a>=97 and a<=122) Then

s = s & c

ElseIf a>0 and a<16 Then

s = s & "%0" & Hex(a)

ElseIf a>=16 and a<256 Then

s = s & "%" & Hex(a)

Else

s = s & c

End If

Next

URLEncode = s

End Function


Function URLDecode(input)

Dim str,code,a(3),b(1)

str=""

code=input

code=Replace(code,"+"," ")

Const hexstr = "0123456789ABCDEF"

While len(code)>0

If InStr(code,"%")>0 Then

str = str & Mid(code,1,InStr(code,"%")-1)

code = Mid(code,InStr(code,"%"))

a(0) = UCase(Mid(code,2,1))

a(1) = UCase(Mid(code,3,1))

If a(1)<>"" And InStr(hexstr,a(0))>0 And InStr(hexstr,a(1))>0 Then

b(0) = Eval("&H" & a(0) & a(1))

If b(0)>127 And Mid(code,4,1)="%" And len(code)>=6 Then

a(2) = UCase(Mid(code,5,1))

a(3) = UCase(Mid(code,6,1))

If a(3)<>"" And InStr(hexstr,a(2))>0 And InStr(hexstr,a(3))>0 Then

b(1) = Eval("&H" & a(2) & a(3))

str = str & chr(b(0)*256+b(1))

code = Mid(code,7)

Else

str = str & "%"

code = Mid(code,2)

End If

Else

str = str & chr(b(0))

code = Mid(code,4)

End If

Else

str = str & "%"

code = Mid(code,2)

End If

Else

str = str & code

code = ""

End If

Wend

URLDecode = str

End Function

</script>

<script language="javascript">

function encodeURIComponent(code){

return URLEncode(code);

}

function decodeURIComponent(code){

return URLDecode(code);

}

function encodeURI(code){

return URLEncode(code);

}

function decodeURI(code){

return URLDecode(code);

}

</script>

--------------------------------本文属转载-------------------------------------------------------------------------------

你也可以对单个字符串进行编码,后经传递接收后在解码就可轻松解决该问题,不妨试试

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值