js在URL中傳參中文出現亂碼
最近用easyUI做一個下拉框,在輸入某值後根據某值模糊查詢:
$('#comboboxA').combobox({
url:basePath+'form/formListForCombobox',
method:'get',
valueField:'formId',
textField:'formDisplayName',
panelHeight:200,
multiple:false,
keyHandler: {
up: function (e) { //【向上鍵】押下處理
},
down: function (e) { //【向下鍵】押下處理
},
enter: function (e) { //【斷行符號鍵】押下處理
var inputText = $('#comboboxA').combobox('getText');
var url = basePath+'form/searchFormList'+'?inputText='+ inputText;
$('#comboboxA').combobox('reload',url);
$('#comboboxA').combobox('setValue',inputText);
/*$.ajax({
type:'post',
url:basePath+'form/searchFormList',
data:{'inputText':inputText},
dataType:'json',
success:function (formOptionList){
console.log(formOptionList);
},
error: function(result) {
if(result != 'null' && result != ''){
$.messager.alert("提示", "出現未知錯誤!");
}
}
});*/
},
query: function (keyword) { //【動態搜尋】處理
}
}
});
其中的var url = basePath+'form/searchFormList'+'?inputText='+ inputText;
這一行用到了get方式傳參(url後面直接跟參數為get方式,將參數放在data{"inputText":"inputText"}為post方式傳參)。
而如果沒有配置Servers\Tomcat v7.0 Server at localhost-config\server.xml的約64行:
則會出現中文亂碼。
此亂碼還有一種解決方案:
js中:var url = basePath+'form/searchFormList'+'?inputText='+ encodeURI(inputText,"UTF-8");
controller中: String inputTextVal = new String(inputText.getBytes("ISO-8859-1"),"UTF-8");
不過,第一種方法比較推薦,直接在配置裡配置一下方便。