JS solves Chinese garbled characters in url values

Source: Internet
Author: User

During web application development, data interaction between the front and back ends is required in many cases. Sometimes we may need to transmit the Chinese data from the front end to the back end through URL, but at this time there is a headache, Because Java Network Transmission uses the standard character set is ISO-8859-1, so in the background with request. getParameter ("message"); get the front-end passed Chinese, the obtained is the ISO-8859-1 Character Set, Chinese will appear garbled phenomenon, the solution for many people is to perform decode before the front-end transmits Chinese characters, and then decode the back-end for conversion. This is very troublesome. Isn't there any other solution?

I thought for a moment: why can I transmit Chinese characters in the form mode, but the URL mode cannot be used? Can the problem be solved only when URL-based value passing is required? Here I think of a dynamic form. Why not use it? The example is as follows:

For example, in the following scenarios: On the KPI list page, the alarm data is generated for each KPI. When you click the button for generating data for each KPI in the list, you need to pass the KPI id and name to the background.

Js method corresponding to the frontend:

Use the URL to pass the value:

Copy codeThe Code is as follows: function createData (indexId, indexName ){
Window. location. href = "catalogAction. do? Action = CreateIndexData & catalogId = "+ indexId +" & catalogName = "+ indexName +" & random = "+ Math. random ();


In this way, the KPI name is mostly Chinese, and the value received in the background must be garbled.

If you use the dynamic form generation method, you can solve the problem:

Copy codeThe Code is as follows: function createData (indexId, indexName ){
Var urlStr = "catalogAction. do? Action = CreateIndexData & PcatalogId = <c: out value = '$ {catalogId}'/> & catalogId = "+ indexId +" & random = "+ Math. random ();
Var f = document. createElement ('form ');
F. action = urlStr;
F. method = 'post ';
Document. body. appendChild (f );
Var temp = document. createElement ('input ');
Temp. type = 'ddn ';
Temp. value = indexName;
Temp. name = 'catalogname ';
F. appendChild (temp );
F. submit ();

The Chinese field is submitted in form mode. If it is not Chinese, the URL is still used for transmission. This easily solves the problem of garbled Chinese characters passed to the background.

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.