Home > Web Front-end > JS Tutorial > How to solve the jquery parameter garbled problem

How to solve the jquery parameter garbled problem

藏色散人
Release: 2022-12-30 11:11:31
Original
2423 people have browsed it

Methods to solve garbled jquery parameters: 1. Transcode through "new String(param.getBytes("iso8859-1"), "utf-8");"; 2. The request to modify the page is Just POST request.

How to solve the jquery parameter garbled problem

## The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer.

Recommendation:

jQuery video tutorial

jQuery sends a request to transmit garbled Chinese parameters

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

The menu is as follows:


How to solve the jquery parameter garbled problem

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) to send requests to the background, because The parameters are sent directly in GET mode, so the browser encodes the parameters using URL encoding, and the parameters obtained in the background are:

<br/>How to solve the jquery parameter garbled problem

You can see that the param received is garbled. . 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
<br/>
Copy after login

The above is the detailed content of How to solve the jquery parameter garbled problem. 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