`
simgsg
  • 浏览: 91622 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

GBK编码下jQuery Ajax中文乱码终极暴力解决方案

 
阅读更多
<p>哎,还是老话题,一直缠绕着国人……</p>
<p>现在来个快刀斩乱麻,let’s go<br>由于项目前期使用的是1.2.6版本,后期使用的是1.3.2版本,所以分开来讲;</p>
<p>说说实现的办法,有点暴力——直接修改jQury源码,不过不用担心,不会影响整体功能而且就修改几行代码就可以解决乱码,也就是在发送请求的时候把参数值再进行一次转码,修改的方法为<em><strong>param</strong></em></p>
<p></p>
<p>&lt;!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dt--&gt;&lt;!-- change this for your own feed url --&gt;1、jQuery <strong>1.2.6</strong> 乱码解决办法</p>
<ul>
<li>打开1.2.6版本的源文件 </li>
<li>找到第2911、2921、2924行,分别修改对应参数值的地方包裹一层encodeURIComponent即可,修改后的结果如下:
<div class="wp_syntax">
<div class="code"><textarea cols="70" rows="16" name="code" class="javascript">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] &amp;&amp; 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("&amp;").replace(/%20/g, "+");
}
</textarea></div>
</div>
</li>
</ul>
<p class="wp_syntax">
</p>
<div class="code">下载:<a title="版本:1.2.6,类别:[jQuery],描述:为GBK乱码问题修改的1.2.6版本" href="http://www.wsria.cn/wordpress/wp-content/plugins/download-monitor/download.php?id=19">jquery-encode-gbk.1.2.6.js</a> <br>下载:<a title="版本:1.2.6,类别:[jQuery],描述:压缩过的版本1.2.6,解决GBK乱码问题" href="http://www.wsria.cn/wordpress/wp-content/plugins/download-monitor/download.php?id=21">jquery-encode-gbk.pack.1.2.6.js</a> </div>

<p></p>
<p>2、jQuery <strong>1.3.2 </strong>乱码解决办法</p>
<p>1.3.2的就不用这么麻烦了,因为这个版本简化了param方法的结构,只需要修改一行代码即可,因为只有一个地方转码了</p>
<p>找到第3737行,同样包裹一层encodeURIComponent,结果如下:</p>
<p><textarea cols="76" rows="9" name="code" class="javascript">param: function( a ) {
  var s = [ ];

  function add( key, value ){
   s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(encodeURIComponent(value));
  };
</textarea></p>
<div class="wp_syntax">
<div class="code">下载:<a title="版本:,类别:[jQuery],描述:为GBK乱码问题修改的1.3.2版本" href="http://www.wsria.cn/wordpress/wp-content/plugins/download-monitor/download.php?id=20">jquery-encode-gbk-1.3.2.js</a> <br>下载:<a title="版本:1.3.2,类别:[jQuery],描述:压缩过的版本1.3.2,解决GBK乱码问题" href="http://www.wsria.cn/wordpress/wp-content/plugins/download-monitor/download.php?id=22">jquery-encode-gbk-.pack.1.3.2.js</a> </div>
</div>
&lt;!-- 雅虎流量统计 结束--&gt;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics