Js parameters garbled

In the foreground, performed twice on the Chinese encodeURI parameters of the URL:

 序列化 var param = encodeURI(encodeURI("中文"));


反序列化 decodeURI($.request("modeName"));

URLDecoder decoding using background data fetch:

  1.  
    String param = (String)request.getParameter("param");
  2.  
    param = URLDecoder.decode(param, "UTF-8");

Analytical principle

Reception pass parameters "Chinese":

First the encodeURI, acquires an array of bytes into utf-8 according to Mode [-28, -72, -83, -34, -67, -41], bytecode traverse the array, each byte is converted to to the corresponding hexadecimal number, which becomes [e4, b8, ad, e6,96,87], and finally into [% e4,% b8,% ad,% e6,% 96,% 87].

Second encodeURI, and finally into the array [% 25e4,% 25b8,% 25ad,% 25e6,% 2596,% 2587] and then put the processed data [% 25e4,% 25b8,% 25ad,% 25e6,% 2596, 2587%] sent to the backend server side.

When the server calls the method getParameter acquisition parameters, the server reads pass over the front [% 25e4,% 25b8,% 25ad,% 25e6,% 2596,% 2587], then perform a URLdecode operation, then the operation result [% e4,% b8,% ad,% e6,% 96,% 87] back to getParameter, this time can perform another URLdecode to restore the original data into a page sent from the "Chinese" a.

URL encoding and twice encodeURI

When submitting a query parameter using the address bar, if not coded, non-English characters in accordance with the operating system perform character set encoding submitted to the server, the server will be decoded according to the character set configuration, so if they do not agree will lead to garbled.

encodeURI function takes a URL address UTF-8 encoded, so if the server used during decoding is another encoding will be garbled, the default character set decoding server configuration is not UTF-8, so in most cases It will produce garbled bar submitting Chinese query parameters; for this case, can be used continuously twice encodeURI (mainly refers to the browser) to non-English characters are encoded on the client, and then use the service end the Java the .NET .URLDecoder (String. "UTF-8") decoder, you can get the right Chinese.

If only once encodeURI, UTF-8 is obtained in the form of URL, the server through request.getParameter () query parameter decoding (usually iso-8859-1) get distortion.

If two encodeURI, first obtained by encoding is UTF-8 in the form of URL, the second coding still gets UTF-8 in the form of URL, but the equivalent of first conducted a UTF-8 encoded in effect ( At this point all that has been converted to ASCII characters), and then an iso-8859-1 encoding, the same as the English character effect is UTF-8 encoded and the encoded ISO-8859-1. On the server side, by first automatically request.getParameter () first decoding (probably gb2312, gbk, utf-8, iso-8859-1 other character sets, no effect on the results) obtained ascii characters, then use UTF -8 decoding a second, usually Java .NET .URLDecoder ( "", "UTF-. 8") method.

Decoding two encoded twice process:

UTF-8 encoding -> UTF-8 (iso-8859-1) encoding -> iso-8859-1 decoding -> UTF-8 decoding, encoding and decoding process is symmetrical, distortion does not occur.

encodeURL function is mainly to do the transcoding of the URI, which is coded using a default of UTF-8.
UTF-8 encoding format: a character constituted by three bytes, each byte is converted to hexadecimal encoding, while the number of added%

Guess you like

Origin www.cnblogs.com/zengpeng/p/11590664.html