Home >Web Front-end >JS Tutorial >How to solve the jquery parameter garbled problem
## The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer. Recommendation: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.
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:<br>
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='" + list[i] + "' "; if (department && list[i] == department) { departmentSelector += "selected='selected'"; } departmentSelector += ">" + list[i] + "</option>"; } $("#accountDepartmentAdd").html(departmentSelector); });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>
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");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='" + list[i] + "' "; if (department && list[i] == department) { departmentSelector += "selected='selected'"; } departmentSelector += ">" + list[i] + "</option>"; } $("#accountDepartmentAdd").html(departmentSelector); } });
<br>
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!