这个问题一直困扰着我,之前忽悠过去了,今天重新正视这个问题,而且随着时间的推移,网上的解决方案也随之增多,总结了一下,试验了一下,终于成功了!于是乎在这里跟大家一起分享~~~
实现的方法有点暴力——直接修改jQuery的js代码,但是非暴力的我试过总是不行(可能我能力有限吧~)废话不罗嗦,直接切入正题吧。
1、jQuery 1.2.6 乱码解决办法
- 打开1.2.6版本的源文件
- 找到第2911、2921、2924行,分别修改对应参数值的地方包裹一层encodeURIComponent即可,修改后的结果如下:
var s = [];
// If an array was passed in, assume that it is an array
// of form elements
if ( a.constructor == Array || a.jquery )
// Serialize the form elements
jQuery.each( a, function(){
s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent(encodeURIComponent( this.value )) );
});
// Otherwise, assume that it's an object of key/value pairs
else
// Serialize the key/values
for ( var j in a )
// If the value is an array then the key names need to be repeated
if ( a[j] && a[j].constructor == Array )
jQuery.each( a[j], function(){
s.push( encodeURIComponent(j) + "=" + encodeURIComponent(encodeURIComponent( this )) );
});
else
s.push( encodeURIComponent(j) + "=" + encodeURIComponent(encodeURIComponent( jQuery.isFunction(a[j]) ? a[j]() : a[j] )) );
// Return the resulting serialization
return s.join("&").replace(/%20/g, "+");
}
2.jQuery 1.3.2乱码解决方法
1.3.2的就不用这么麻烦了,因为这个版本简化了param方法的结构,只需要修改一行代码即可,因为只有一个地方转码了
找到第3737行,同样包裹一层encodeURIComponent,结果如下:
param: function( a ) {
var s = [ ];
function add( key, value ){
s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(encodeURIComponent(value));
};
在jsp端
1、用post方法提交:客户端需编码数据,data : encodeURI(spost),
var spost="commentContent="+commentContent+"&contentId="+contentId;
spost = encodeURI(spost);
在action端
用psot方法取值,客户端需编码,服务器端需解码
String ii = request.getParameter("commentContent");
ii = java.net.URLDecoder.decode(ii,"utf-8");
2、用get方式
jsp端
type:'GET',不需要处理
action端
String commentContent = new String((request.getParameter("commentContent")).getBytes("ISO-8859-1"),"UT简单的记录一下
//我是豆豆D分割线/
再补充一个,刚遇到这么一个问题,struts.xml和web.xml下都有编码设置:
struts.xml:<constant name="struts.i18n.encoding" value="GBK" />
web.xml:
<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>
</filter>
即使这里设置好了,传到action之后仍有问题,还有一个方法,就是在JSP页面上转两次码:
feedContent = encodeURI(encodeURI(feedContent));
然后在action里面加这句话就可以了: