IE下get傳中文亂碼的問題完美解決方案

前幾天作項目的時候遇到須要在easyui的combobox的url中以get的方式傳中文,出現亂碼。javascript

$('#cc').combobox({
  url : 'xxxAction.action?para='+中文,
  editable : false,
  valueField : 'cityId',
  textField : 'cityName'
});

到網上尋求解決方案。但基本就是那幾種解決方案。java

1.在Tomcat的server.xml文件Connector標籤中加入URIEncoding= "UTF-8"。web

2.在web.xml中加入spring處理中文的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");json

4.新建個過濾器。app

以上4種方法我都試過,firefox和Chrome,都已經解決了,可是IE下死活就是很差使,亂碼依然存在。post

這時我第一個辦法是,若是get不行,就改爲post吧,只好改寫combobox的方式,用combobox的loader功能。ui

$('#cc').combobox({
    loader : function(param,success,error){
        $.post(
	    "xxxAction.action", 
	    { provinceId: provinceId },
	    function(data){
	        success(data);
	    }, 
	    "json"
	);
    },
  editable : false,
  valueField : 'cityId',
  textField : 'cityName'
});    

後來跟同事一塊兒討論的時候,一位同事又給了我另外一種解決方案,使用encodeURIurl

var url = encodeURI('xxxAction.action?para='+中文);
$('#cc').combobox({
    url : url,
    editable : false,
    valueField : 'cityId',
    textField : 'cityName'
});

仍是第二種方式更簡單,使用更靈活。firefox

相關文章
相關標籤/搜索