Home > Web Front-end > JS Tutorial > How to avoid garbled characters in encodeURI(url) in JS?

How to avoid garbled characters in encodeURI(url) in JS?

php中世界最好的语言
Release: 2018-03-09 10:52:42
Original
2690 people have browsed it

This time I will show you how to avoid garbled characters in encodeURI(url) in JS? , what are the precautions when using encodeURI(url) in JS? The following is a practical case, let’s take a look.

Generally, send encodeURIComponent(parmeName)+"="+encodeURIComponent(parmeValue);
When receiving, directly String paramValue = request.getParameter(paramName); // The container automatically decodes.

We know that encodeURIComponent is compiled using UTF-8 encoding rules.
If the container is also decoded according to UTF-8 when request.getParameter(paramName), it is correct. There is no need to perform a second encodeURIComponent(...) on the client

If the container does not decode request.getParameter(paramName) according to UTF-8, there will be only one result, which is garbled code!
The encoding used by the container to decode depends on request.setCharacterEncoding(***) or server program configuration.

If you are in a jsp program, you can request.setCharacterEncoding("UTF-8") and modify it Server configuration allows the container to use UTF-8 when decoding the parameters submitted by GET.

The client does not need to re-encode before submitting. When receiving, it only needs to directly request.getParameter(paramName)

Why do some people on the Internet propose to repeatedly encode the string twice on the client side?
If the project needs it, it is not possible to specify which encoding rules the container uses to decode the submitted parameters, such as: When you need to receive parameter content from different pages without local encoding. (Or maybe the developer is confused by this somewhat complicated stuff and doesn't know how to correctly do the work of receiving parameters)
At this time, secondary encoding of parameters on the client can be effective Avoid the tricky problem of "committing multibyte characters".
Because of the first encoding, your parameter content does not contain multi-byte characters and becomes a pure Ascii string. (Here, the result of the first compilation is called [STR_ENC1]. [STR_ENC1] does not contain multi-byte characters)
After compiling again, submit, and the container will automatically resolve it once when receiving (the container will automatically resolve it This time, no matter whether you press GBK, UTF-8 or ISO-8859-1, you can get [STR_ENC1] correctly)
Then, implement decodeURIComponent again in the program (java.net is usually used in Java .URLDecoder(***, "UTF-8")) to get the original value of the parameter you want to submit.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese website Other related articles!

Related reading:

A brief introduction to the principle of clearfix in CSS

Common properties of CSS1 and CSS2

How to use CSS3 to create a login box

The above is the detailed content of How to avoid garbled characters in encodeURI(url) in JS?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template