Home>Article>Web Front-end> What should I do if jQuery sends a request to transmit garbled Chinese parameters?

What should I do if jQuery sends a request to transmit garbled Chinese parameters?

coldplay.xixi
coldplay.xixi Original
2020-12-03 10:41:42 1898browse

The solution to the jQuery request to transmit garbled Chinese parameters: first send the parameters directly in GET mode; then transcode, the code is [String center = new String(param.getBytes("iso8859-1") , "utf-8")].

What should I do if jQuery sends a request to transmit garbled Chinese parameters?

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, Dell G3 computer.

Solution to the jQuery request to transmit garbled Chinese parameters:

The requirements I am currently working on involve cascading queries, and the query needs to be based on the content of the upper-level drop-down box. Display the lower-level drop-down box list. Because there are only two levels of cascade, and the data in the table will hardly be changed later, the table I designed stores directly Chinese.

The menu is as follows:

What should I do if jQuery sends a request to transmit garbled Chinese parameters?

The code is as follows:

var url = "${basePath}/institutionConfig/getDepartmentByCenter.do?param=" + center; $.get(url, function (data) { var list = data.data; for (var i = 0; i < list.length; i++) { departmentSelector += "

I use$.get(url, callback)When sending a request to the background, since the parameters are sent directly in GET mode, the browser encodes the parameters with URL encoding, and the parameters obtained by the background are:

What should I do if jQuery sends a request to transmit garbled Chinese parameters?

As you can see, param received garbled characters. So I performed further processing, that is, transcoding:

String center = new String(param.getBytes("iso8859-1"), "utf-8");

so that the received text is Chinese.

However, this approach actually reported an error in the test environment. After analyzing the reason, I found that the test environment received the correct Chinese, but it turned out to be wrong after transcoding. Therefore, the solution should be to change the page request. Because the parameters caused by the GET method are encoded, it is changed to a POST request. The POST request will submit the original data:

var url = "${basePath}/institutionConfig/getDepartmentByCenter.do"; $.ajax({ url: url, data: {"param": center}, dataType: "json", type: "POST", success: function (data) { var list = data.data; for (var i = 0; i < list.length; i++) { departmentSelector += "

Related free learning recommendations:javascript(Video)

The above is the detailed content of What should I do if jQuery sends a request to transmit garbled Chinese parameters?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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