哎,还是老话题,一直缠绕着国人……
现在来个快刀斩乱麻,let’s go
由于项目前期使用的是1.2.6版本,后期使用的是1.3.2版本,所以分开来讲;
说说实现的办法,有点暴力——直接修改jQury源码,不过不用担心,不会影响整体功能而且就修改几行代码就可以解决乱码,也就是在发送请求的时候把参数值再进行一次转码,修改的方法为param
1、jQuery 1.2.6 乱码解决办法
- 打开1.2.6版本的源文件
- 找到第2911、2921、2924行,分别修改对应参数值的地方包裹一层encodeURIComponent即可,修改后的结果如下:
param: function( a ) {
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, "+");
}
资源下载:jquery-encode-gbk.1.2.6.js 共 764 次
资源下载:jquery-encode-gbk.pack.1.2.6.js 共 653 次
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));
};
资源下载:jquery-encode-gbk-1.3.2.js 共 1312 次
资源下载:jquery-encode-gbk-.pack.1.3.2.js 共 1255 次
重要说明:后台必须要转码:java.net.URLDecoder.decode(request.getParameter(“name”), “UTF-8″)
一只咖啡兔,热爱开源,喜欢追踪、研究新技术,向往背包游……
讲究效率,坚信:“工欲善其事必先利其器”
使用Linux(Ubuntu),基于Java和jQuery开发企业RIA应用
More Posts - Website
哇,又有评论了,有你吗?