首页 > jQuery, 前端技术, 后台技术, 教程 > 站点GBK编码下jQuery Ajax中文乱码解决方案

站点GBK编码下jQuery Ajax中文乱码解决方案

哎,还是老话题,一直缠绕着国人……

现在来个快刀斩乱麻,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 共 489 次
    下载:jquery-encode-gbk.pack.1.2.6.js 共 405 次

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 共 854 次
下载:jquery-encode-gbk-.pack.1.3.2.js 共 847 次

重要说明:后台必须要转码:java.net.URLDecoder.decode(request.getParameter(“name”), “UTF-8″)

Popularity: 100%

  1. 小虎子 Aug 2nd, 2010 @ 16:47 | #1

    楼主。我就是不想动服务器才想着改js的。。汗。。最后还是要加个url的转码。。晕。。

  2. mefly Jun 10th, 2010 @ 10:12 | #2

    encodeURIComponent(encodeURIComponent(value))
    为什么您要encodeURIComponent 2次呢,请解释下,谢谢

  3. bh Apr 10th, 2010 @ 19:00 | #3

    有没有1.4.2版本的

  4. 辅导费 Mar 20th, 2010 @ 14:43 | #4

    我的是gb2312改了还是乱码

  5. 路过 Mar 12th, 2010 @ 15:42 | #5

    @swit1983, 前后台都要做转码

  6. 黄石 Mar 3rd, 2010 @ 18:52 | #6

    谨向楼主请教:
    我有两个文件,一个html页面test.html,代码如下:

    报表任务参数

    $(document).ready(
    function(){
    $.ajax({
    type:”GET”,
    url:”test.js”,
    dataType: “script”,
    scriptCharset:”GBK”
    });
    })

    另一个是test。js文件:
    alert(“china中华”);

    我在浏览器中运行test.html文件,弹出的对话框中的中文总为乱码,请教楼主,有何好办法?

  7. 无聊 Mar 2nd, 2010 @ 21:13 | #7

    @过客
    我也想要

  8. 无聊 Mar 2nd, 2010 @ 21:13 | #8

    @过客, 我也想要 :|

  9. 过客 Feb 25th, 2010 @ 08:51 | #9

    jquery 1.4版的改那里呢?

  10. linjuming Jan 15th, 2010 @ 20:22 | #10

    博主帮帮忙,看看我这帖子
    http://topic.csdn.net/u/20100115/10/6eb9801d-3d8c-48e3-8679-2a6ea142114a.html
    gbk静态网页怎么获取其它gbk静态网页的内部内容?我用你的gbk js获取的结果还是乱码

    • wsria Jan 16th, 2010 @ 00:24 | #11

      这个问题我遇到过,你在source.html中也设置字符编码为GBK就可以了,因为AJAX要根据页面的编码来转码,如果没有设置就是用默认的ISO8859_1,所以才会出现乱码问题
      不过建议开发时尽量全站是用UTF-8编码,也不用考虑会有乱码的情况发生,因为Ajax默认发送的就是UTF-8编码
      @linjuming

  11. 王子 Dec 1st, 2009 @ 09:44 | #12

    哈哈,博主,我觉得你的这篇文章需要改了,我下载的版本是jQuery 1.3.2,编码统一用utf8,但是就没有乱码的问题。

    • admin Dec 1st, 2009 @ 15:51 | #13

      O(∩_∩)O~,我写的是在GBK编码情况下,UTF-8当然没有问题了

  12. 初学者 Aug 22nd, 2009 @ 16:27 | #14

    请问博主,jquery-encode-gbk-.pack.1.3.2.js 怎么调用啊?在哪儿调用?

    • admin Aug 22nd, 2009 @ 18:18 | #15

      直接引用就是了

  13. swit1983 Aug 17th, 2009 @ 17:22 | #16

    为什么用后台就不用转码了呢?ajax post 方式提交的数据难道不会被服务器解码吗?
    java中的filter也不会解码吗?

  14. geek87 Jul 7th, 2009 @ 10:18 | #17

    刚学看不明白,怎么改

    • admin Jul 7th, 2009 @ 10:25 | #18

      呵呵,那就直接下载我打包的文件

      • geek87 Jul 7th, 2009 @ 10:34 | #19

        谢谢LZ 你哪个包写了一串 东西。。看了头晕。。什么东西呀

        • admin Jul 7th, 2009 @ 10:46 | #20

          我是分了两个版本的,一个是1.26的另外一个是1.3.2版本的,你现在用的是哪个版本的 jQuery就下载对应的版本
          上面的代码是在文件中修改的,在哪一行中怎么修改都写了
          应该没有那么难理解吧……

评论提交中, 请稍候...

留言

可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
Trackbacks & Pingbacks ( 2 )
  1. Feb 11th, 2010 @ 11:21 | #1
    Pingback: 一地鸡毛 » 站点GBK编码下jQuery Ajax中文乱码解决方案(zz)
  2. Jun 22nd, 2010 @ 15:57 | #2
    Pingback: 站点GBK编码下jQuery Ajax中文乱码解决方案 | Web development notes