溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

jQuery發(fā)請求傳輸中文參數(shù)亂碼怎么解決

發(fā)布時(shí)間:2022-03-31 10:57:18 來源:億速云 閱讀:376 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“jQuery發(fā)請求傳輸中文參數(shù)亂碼怎么解決”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“jQuery發(fā)請求傳輸中文參數(shù)亂碼怎么解決”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

菜單如下:

jQuery發(fā)請求傳輸中文參數(shù)亂碼怎么解決

代碼如下:

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);
});

我用的是$.get(url, callback)向后臺發(fā)送請求的,由于將參數(shù)直接以GET方式發(fā)送,所以瀏覽器對參數(shù)用URL編碼進(jìn)行了encode,而后臺獲取到的參數(shù)為:

jQuery發(fā)請求傳輸中文參數(shù)亂碼怎么解決

可以看到,param接收到的是亂碼。所以我進(jìn)行了進(jìn)一步的處理,即轉(zhuǎn)碼:

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

這樣接收到的就是中文了。

但是這樣的做法在測試環(huán)境竟然報(bào)錯(cuò)了,分析了下原因,發(fā)現(xiàn)測試環(huán)境接收到的就是正確的中文,轉(zhuǎn)碼后反而錯(cuò)了。所以,解決方案應(yīng)該是改頁面的請求。因?yàn)槭褂昧薌ET方式導(dǎo)致的參數(shù)被編碼了,所以改為POST請求,POST請求會(huì)提交原始數(shù)據(jù):

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);
  }
 });

讀到這里,這篇“jQuery發(fā)請求傳輸中文參數(shù)亂碼怎么解決”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI