设为首页收藏本站

就爱编程论坛

 找回密码
 注册

人人连接登陆

无需注册,直接登录

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

查看: 376|回复: 0
打印 上一主题 下一主题

ajax中文乱码问题 [复制链接]

Rank: 3Rank: 3

升级 
 
97%
  • TA的每日心情

    2012-8-22 08:34:27
  • 签到天数: 16 天

    [LV.4]偶尔看看III

    跳转到指定楼层
    楼主
    发表于 2011-4-1 04:48:38 |只看该作者 |倒序浏览
    初学Ajax,在做实例时,发现了一个问题,就是使用Ajax获取中文时,会产生乱码,在此请教各位。

    实例代码如下所示:

    <html>
    <head>
    <title> Ajax实例 </title>
    <script   language= "javascript "   type= "text/javascript ">
    <!--
    var   xmlHttp;
    function   createXMLHttpRequest()
    {
    if   (window.ActiveXObject)
    {
    xmlHttp   =   new   ActiveXObject( "Microsoft.XMLHTTP ");
    }
    else   if   (window.XMLHttpRequest)
    {
    xmlHttp   =   new   XMLHttpRequest();
    }
    }

    function   httpStateChange()
    {
    if   (xmlHttp.readyState==4)
    {
    if   (xmlHttp.status==200   ||   xmlHttp.status==0)
    {
    var   node   =   document.getElementById( "myDiv ");
    node.firstChild.nodeValue   =   xmlHttp.responseText;
    }
    }
    }

    function   getData()
    {
    createXMLHttpRequest();
    if   (xmlHttp!=null)
    {
    xmlHttp.open( "get ", "ajax.txt ",true);
    xmlHttp.onreadystatechange   =   httpStateChange;
    xmlHttp.send(null);
    }
    }
    -->
    </script>
    </head>
    <body>
    <div   id= "myDiv "> 原数据 </div>
    <input   type= "button "   value= "更新数据 "  >
    </body>
    </html>

    获取的ajax.txt文件与hmtl文件放在同一个文件夹下。
    如果将ajax.txt在记事本中存为ansi编码,则使用IE6、opera9、netscape7和firefox2打开为乱码。
    如果将ajax.txt在记事本中存为unicode编码和Unicode   big   endian编码,则使用IE6、opera9都能正常打开,但是使用netscape7和firefox2,打开为乱码。
    如果将ajax.txt在记事本中存为utf-8编码,则使用IE6、opera9和firefox2都能正常打开,但是使用netscape7打开为乱码。

    最后总结是,无论将ajax.txt文件存为什么编码,Netscape7都不能将中文正确显示出来。请问,该问题要如何解决?
    =====================================================================================
    如果将ajax.txt在记事本中存为utf-8编码,则使用IE6、opera9和firefox2都能正常打开,但是使用netscape7打开为乱码。

    那就放弃netscape7吧O.o
    =====================================================================================
    function   getData()
    {
    createXMLHttpRequest();
    if   (xmlHttp!=null)
    {
    xmlHttp.open( "get ", "ajax.txt ",true);
    xmlHttp.onreadystatechange   =   httpStateChange;
    xmlHttp.send(null);
    }
    }


    改成:
    function   getData()
    {
    createXMLHttpRequest();
    if   (xmlHttp!=null)
    {
    xmlHttp.open( "get ", "ajax.txt ",true);
    xmlHttp.setRequestHeader( 'Content-type ', 'application/x-www-form-urlencoded ');
    xmlHttp.onreadystatechange   =   httpStateChange;
    xmlHttp.send(null);
    }
    }
    试试
    ========================================================================================
    分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    分享分享0 收藏收藏0 支持支持0 反对反对0 分享到人人 转发到微博

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册 人人连接登陆

    晴云孤魂's Blog|就爱编程搜帖|手机版|Archiver|就爱编程论坛     

    GMT+8, 2025-7-2 10:49 , Processed in 0.098256 second(s), 25 queries .

    Powered by Discuz! X2

    © 2001-2011 Comsenz Inc.

    回顶部