您的位置:首页 > 其它

AJAX中关于responseText乱码问题

2006-06-21 17:20 471 查看
用AJAX来GET回一个页面时,RESPONSETEXT里面的中文多半会出现乱码,这是因为xmlhttp在处理返回的responseText的时候,是把resposeBody按UTF-8编码进解码考形成的,如果服务器送出的确实是UTF-8的数据流的时候汉字会正确显示,而送出了GBK编码流的时候就乱了。解决的办法就是在送出的流里面加一个HEADER,指明送出的是什么编码流,这样XMLHTTP就不会乱搞了。

PHP:header('Content-Type:text/html;charset=GB2312');
ASP:Response.Charset("GB2312")
JSP:response.setHeader("Charset","GB2312");

checkname1.html
---------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>帐号检测</title>
</head>
<body>
<script type="text/javascript" language="javascript">
var http_request = false;
function makeRequest(url) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.send(null);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {

document.getElementById('aa').innerHTML=http_request.responseText;

//hello=http_request.responseText;
//document.getElementById('aa').innerHTML=http_request.responseText;
} else {
alert('There was a problem with the request.');
}
}
}

function userCheck() {
var f = document.form1;
var username = f.username.value;
if(username=="") {
window.alert("用户名不能为空。");
f.username.focus();
return false;
}
else {
makeRequest('check_ok.asp?username='+username);
}
}

</script>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<form name="form1">
<tr>
<td width="45%">
<div align="right">用户名:
<input name="username" type="text" id="username" onBlur="userCheck()">
</div>
</td>
<td width="55%"><div id="aa" style="height:20px; width:200px; background-color: #BBBBFF ; color: #FFFFFF"></div>
</td>
</tr>
</form>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>

---------check_ok.asp
<% response.Charset="GB2312" '关键在这里%>
<%
u_name=request("username")'这里可以用数据库技术进行相关处理
if u_name="spell" then
response.Write(u_name&"已经被注册")
else
response.Write("恭喜,您选的帐号 "&u_name&" 可以注册!")
end if
%>

出处:http://www.ecspell.com/post/101.html

-------------------------

最近在用AJAX+PHP+MYSQL+APACHE写一个小小的留言本
算是对AJAX的综合应用迈出了一小步
在制作过程中有很多兴奋的体验 虽然和以前的制作方法比起来繁杂了一些
但是整个页面的结构和数据确实完全分开了 维护起来也更加容易
不过就在我成功搭建了ajax的运转平台以后(包括一些读取数据的js函数)发现读取出来的中文数据竟然都是乱码
一下子就把我的兴奋给浇灭了。。。
马上google了一下 发现原来ajax默认的是用utf-8的编码发送和接收数据的
而我的页面charset是gb2312的 因此出现乱码就不足为奇了
联想起以前在firefox下面读取cookie的时候出现了同样的问题
解决起来就很方便了 只要在js发送数据之前用escape函数把data转换为16进制的字符串保存在数据库里即可
同样 用js读取数据的时候再用unescape转换回来就ok了
这样感觉像在做计算机网络安全的作业。。。。
加密 解密。。。。
不过也好 确实安全性高了一些 至少数据库里面 人家看不到真实的中文资料哈哈哈
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: