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

JS alternative method to solve the problem of Chinese garbled characters in url value transfer_javascript skills

WBOY
Release: 2016-05-16 17:37:47
Original
1043 people have browsed it

When developing web applications, many situations require data interaction between the front and backends. Sometimes we may need to pass the Chinese data in the frontend to the backend through URLs, but at this time there is a headache, because The standard character set used for network transmission in Java is ISO-8859-1, so when you use request.getParameter("message") in the background to get the Chinese sent from the front, you still get the ISO-8859-1 character set, and the Chinese is There will be garbled characters. The solution for many people is to decode before transmitting Chinese in the foreground, and then decode and convert it in the background. This feels very troublesome. Is there no other way to solve it?

I thought hard for a while: Why can the form be used to transmit Chinese, but the URL method cannot be used? Do we have to use URL to pass value to solve the problem? Here I thought of dynamic forms, why not use it to solve the problem, the example is as follows:

Suppose there is the following application scenario: In the KPI list page, each KPI has the function of generating early warning data. When you click the button to generate data for each KPI in the list, the id and name of the KPI need to be passed to the background.

The js method corresponding to the front end:

Use URL to pass value:

Copy code The code is as follows:

function createData (indexId,indexName){
                                                                                                                                                                                     window.location.href="catalogAction.do?action=CreateIndexData&catalogId=" indexId "&catalogName=" indexName "&random=" Math.random();

      }


Using this method, since most KPI names are in Chinese, the values ​​received in the background must be garbled.

This can be solved by dynamically generating the form:

Copy the code The code is as follows:

function createData(indexId,indexName){
var urlStr = "catalogAction.do?action=CreateIndexData&PcatalogId=&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= 'hidden';
temp.value=indexName; name='catalogName';
                    f.appendChild(temp); 
                    f.submit(); Easily solve the problem of garbled Chinese characters being transmitted to the background.
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