前幾天做項目的時候遇到需要在easyui的combobox的url中以get的方式傳中文,出現亂碼。
$('#cc').combobox({ url : 'xxxAction.action?para='+中文, editable : false, valueField : 'cityId', textField : 'cityName' });
到網上尋求解決方案。但基本就是那幾種解決方案。
1.在Tomcat的server.xml文件Connector標簽中加入URIEncoding= "UTF-8"。
2.在web.xml中加入spring處理中文的
<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3.用Java處理String亂碼問題,String str= new String(req.getParameter("str").toString().getBytes("iso8859_1"), "UTF-8");
4.新建個過濾器。
以上4種方法我都試過,firefox和Chrome,都已經解決了,但是IE下死活就是不好使,亂碼依然存在。
這時我第一個辦法是,如果get不行,就改成post吧,只好改寫combobox的方式,用combobox的loader功能。
$('#cc').combobox({ loader : function(param,success,error){ $.post( "xxxAction.action", { provinceId: provinceId }, function(data){ success(data); }, "json" ); }, editable : false, valueField : 'cityId', textField : 'cityName' });
后來跟同事一起討論的時候,一位同事又給了我另一種解決方案,使用encodeURI
var url = encodeURI('xxxAction.action?para='+中文); $('#cc').combobox({ url : url, editable : false, valueField : 'cityId', textField : 'cityName' });
還是第二種方式更簡單,使用更靈活。