Ajax获取中文乱码的问题

smallfools 2007-09-27 05:59:28
初学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="更新数据" onclick="getData()">
</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都不能将中文正确显示出来。请问,该问题要如何解决?
...全文
516 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
smallfools 2007-10-08
  • 打赏
  • 举报
回复
试了一下,在netscape 7.2、netscape 8.0、netscape 9.0中都没有问题,就是不知道为什么在7.0中有问题。算了,不管了。撒分。
smallfools 2007-10-08
  • 打赏
  • 举报
回复
晕死,JavaScript中有这些函数吗?
ITshu 2007-10-01
  • 打赏
  • 举报
回复
String val=new String(request.getParameter("val").getBytes("8859_1")); //转化中文
这样应该行了吧
Even713 2007-09-29
  • 打赏
  • 举报
回复
netscape8可以我试过了,不过netscape7的确不可以,关注中
Even713 2007-09-29
  • 打赏
  • 举报
回复
<head>里加一句<meta http-equiv="Content-Type" content="text/html; charset=gb2312">试试
Even713 2007-09-29
  • 打赏
  • 举报
回复
和txt文件编码有关系吗?保存时是否使用了unicode编码?如果使用ansi编码呢?
smallfools 2007-09-29
  • 打赏
  • 举报
回复
2.1. 回转含有中文的数据时,客户端收到的是乱码
原因:  肯定是页面编码的问题,因为我的前提就是不强求使用统一的编码,所以这个问题要解决。
解决:  太简单,只需要在服务端向客户端回写数据前任何地方设置 Response.Chartset = "gb2312" 即可,
     不需要像很多讨论到的要转码甚至有人写出大段的转码程序,当然,客户端如果是别的编码方式,
     改一下就行了。

可是我的代码是直接读取txt文件,因此,在服务器端无法使用Response.Chartset = "gb2312" 语句啊。
smallfools 2007-09-29
  • 打赏
  • 举报
回复
Even713:
和txt文件编码有关系吗?保存时是否使用了unicode编码?如果使用ansi编码呢?
和txt文件编码有关系,使用ansi编码时,所有浏览器打开都是乱码;使用unicode编码时,netscape7和firefox2浏览器为乱码。


<head >里加一句 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" >试试
不要说charset=gb2312了,我连charset=unicode、charset=utf-8都试过了,也不行。

smallfools 2007-09-28
  • 打赏
  • 举报
回复
hejunbin(何俊斌) :
放弃不是最好的选择,再是想弄明白究竟是为什么。
smallfools 2007-09-28
  • 打赏
  • 举报
回复
for_cyan(Cyan) :
nc可以支持中文,因为在html文件中也有中文字符,可以正确显示。只是使用ajax从服务器中返回的中文显示为乱码而已。
hejunbin 2007-09-28
  • 打赏
  • 举报
回复
如果将ajax.txt在记事本中存为utf-8编码,则使用IE6、opera9和firefox2都能正常打开,但是使用netscape7打开为乱码。

那就放弃netscape7吧O.o
for_cyan 2007-09-28
  • 打赏
  • 举报
回复
可能是浏览器的问题,你查看nc支持中文吗
SCUM 2007-09-28
  • 打赏
  • 举报
回复
ajax中文乱码 可以看看以下贴子

http://user.qzone.qq.com/2709575/blog/54
smallfools 2007-09-28
  • 打赏
  • 举报
回复
还是一样的,在Netscape中乱码。
中文乱ç
Even713 2007-09-28
  • 打赏
  • 举报
回复
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);
}
}
试试

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧