Solve the problem of "the decoded URI is not legally encoded"

Source: Internet
Author: User
Tags urlencode

When my colleague used prototype. js for Ajax applications, the script always prompts "the decoded URI is not legally encoded" when the GET request contains Chinese parameters, asking me to solve the problem. This issue has something to do with URL encoding. But in the specific debugging process, it still encountered some trouble and took some detour. Fortunately, it was solved smoothly. My colleague tried to pass unencoded Chinese parameters directly. No, but he tried escape encoding. No. I want to change it.UrlencodeThe function should be okay. I didn't expect it to work, but the error still persists. After someAlert/prompt/WriteThen, the question is determined by the bigPrototype. jsFile. (This JS framework is widely used by many people, but I don't like it. I didn't read it before .) The wrong script line is finally located for a call.DecodeuricomponentThe method is easy to solve. I don't want to change prototype. js. I just want to write my own decodeuricomponent to overwrite it. Found the documentJScript 5.5The following four functions related to Uri encoding and decoding are added:
Encodeuri, decodeuri, encodeuricomponent, decodeuricomponent

The test shows that the four functions (methods) are similar to those in Java and all are encoded and decoded Based on the Unicode standard. That is to say, Chinese characters are treated as three bytes for processing, after a Chinese character is encoded, it becomes a similar form of % HH. Correspondingly, the parameters of the decoding function must also be in the format of the compound encoding result. The two are reversible, otherwise, the error "the decoded URI is not legally encoded" is reported ". While our program commonly used gb2312, GBK, UTF-8 Processing Chinese are 2 bytes, which is the root cause of the problem. For example, in ASP encoded as GBK, useServer. urlencode ("medium ")The result is % D6 % D0, but in the client JSEncodeuri ("medium ")OrEncodeuricomponent ("medium ")The result is % E4 % B8 % ad. If % D6 % D0 is passedDecodeuriOrDecodeuricomponentThe error "the decoded URI is not legally encoded ".

If you don't want to change prototype. JS, write your own function to overwrite the default implementation of the above four methods. Below is the code I wrote. For reference.

<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 ()
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 ()
Elseif A> = 16 and a <256 then
S = S & "%" & hex ()
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 = "0123456789 abcdef"
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) x 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.