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

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

coldplay.xixi
Release: 2023-01-04 09:36:28
Original
1963 people have browsed it

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 += "<option value=&#39;" + list[i] + "&#39; ";
   if (department && list[i] == department) {
    departmentSelector += "selected=&#39;selected&#39;";
   }
   departmentSelector += ">" + list[i] + "</option>";
 }
 $("#accountDepartmentAdd").html(departmentSelector);
});
Copy after login

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");
Copy after login

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 += "<option value=&#39;" + list[i] + "&#39; ";
    if (department && list[i] == department) {
     departmentSelector += "selected=&#39;selected&#39;";
    }
    departmentSelector += ">" + list[i] + "</option>";
   }
   $("#accountDepartmentAdd").html(departmentSelector);
  }
 });
Copy after login

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!

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