Home > Web Front-end > JS Tutorial > body text

Share the problem of garbled characters when JS passes Chinese parameters to Action and how to solve it

零下一度
Release: 2017-04-27 14:55:25
Original
1787 people have browsed it

Action obtains the Chinese parameters in the jsp form. As long as the entire project uses UTF-8 encoding format, there will be no garbled code problem; however, JS is used in JSP, and Chinese parameters are passed from JS to Action, and Chinese garbled characters will appear. Phenomenon

When working on a project, I found that Action obtains Chinese parameters in the jsp form. As long as the entire project uses UTF-8 encoding format, there will be no garbled problem; but JS is used in JSP, and from JS If Chinese parameters are passed to Action, the Chinese characters will be garbled. After asking Baidu several times, there are many opinions.
After practice, we found that the following method can solve the problem of Chinese garbled characters:
In JS of JSP: Chinese parameters use encodeURI (encodeURI (Chinese parameter)), and are transcoded twice. For example:

function show(next,id,realName){ 
document.forms['f2'].action="usersearchNextPage?next="+next+"&id="+id+"&realName="+encodeURI(encodeURI(realName)); 
document.forms['f2'].submit(); 
}
Copy after login

where realName is a Chinese parameter. Therefore, realName is transcoded twice in the submitted URL. encodeURI(encodeURI(realName))
Action: Decode when receiving Chinese parameters. Use: java.net.URLDecoder.decode(realName, "UTF-8");
Such as:

String realName = ServletActionContext.getRequest().getParameter("realName"); 
try { 
realName = java.net.URLDecoder.decode(realName,"UTF-8"); 
} catch (UnsupportedEncodingException e1) { 
e1.printStackTrace(); 
}
Copy after login

After the above processing, the problem is solved.

The above is the detailed content of Share the problem of garbled characters when JS passes Chinese parameters to Action and how to solve it. 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