求教:从SQL数据库读回数据通过AJAX定时显示在页面上时,中文显示乱码问题。

无线空虚EMS 2008-03-07 09:45:18
我想做的是:一个页面不停的刷新数据,数据是从SQL2000读出来的,但是,数据中的中文显示到页面上时是乱码.怎么解决,大头了,我!

HTML页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>Document</title>
<!--begin stylesheet-->
<style type="text/css">
table.news_table
{
border: 1px;
border-style: dotted;
border-color: #00CC99;
border-top: 0;
font-size: 14px;
background: #FFFFFF;
color: #669999;
}
table.news_table thead tr
{
color: #FFFFFF;
background: #0099CC;
border-bottom: 2px solid #FF0000;
padding: 2px 10px;
}
table.news_table tbody tr
{
padding: 2px 10px;
}
</style>
<!--end stylesheet-->
<script type="text/javascript" language="javascript">
var timeout = null; //setInterval函数句柄
var xmlHttp = false; //

//初始化XMLHttpRequest对象
function createXmlHttp()
{
xmlHttp = false;
if (window.ActiveXObject)
{
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
//异步调用update.asp页面获取结果timestamp为了不让浏览器缓存结果
function sendRequest()
{
createXmlHttp();
var url = "update.asp?timestamp=" + new Date().getTime();
if (!xmlHttp)
{
alert("XMLHttpRequest is not Create!");
}
xmlHttp.open("GET", url, true);
//XMLHttpRequest返回的数据是Stream类型,不管你在服务器端怎么编码,客户端只知道那是二进制数据流
//xmlhttp.overrideMimeType("text/html;charset=gb2312");//设定以gb2312编码识别数据
// xmlHttp.setRequestHeader("REFERER", url)
// setRequestHeader("Content-Type","gb2312")
xmlHttp.onreadystatechange = function()
{//回调函数开始
var tag = document.getElementById("container");
tag.innerHTML = "";
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
tag.innerHTML = xmlHttp.responseText;
//xmlHttp.responseText – 以文本字符串的方式返回服务器的响应
} //XMLHttpRequest返回的数据是Stream类型,不管你在服务器端怎么编码,客户端只知道那是二进制数据流,
}//回调函数结束
xmlHttp.send(null);
}
//开始自动刷新
function update()
{
timeout = window.setInterval("sendRequest()", 4000);//设定4秒调用一次update.asp页面
}
//停止自动刷新
function Stopupdate()
{
if (timeout != null)
{
window.clearInterval(timeout);
}
}
</script>
</head>
<body onload="sendRequest();">

<input type="button" value="Start Fresh" onclick="update();"/>
<input type="button" value="Stop Fresh" onclick="Stopupdate();"/>
<div id="container"><!--容器--></div>
</body>
</html>



ASP代码:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Session.CodePage=936%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>

</head>


<body>
<%

dim conn
dim connstr
on error resume next
connstr="Driver={SQL Server};Server=192.168.0.101;UID=sa;PWD=mjt_0755;Database=GQBD"
set conn=server.createobject("ADODB.CONNECTION")
set Rs=Server.CreateObject("ADODB.RecordSet")
conn.open connstr
//sql="select CONVERT(varchar(12) , T_his_submit.d_logdate, 102 ) as d_date , s_mobilecode,(t_his_sm.s_sm)as s_sm,s_MsgContent from T_his_submit left join t_his_sm on T_his_submit.n_oldid = t_his_sm.n_oldid order by T_his_submit.d_logdate desc"
strsql = "select CONVERT(varchar(12) , T_his_submit.d_logdate, 102 ) as d_date , t_his_sm.s_mobilecode,t_his_sm.s_sm,s_MsgContent,n_code from ((T_his_submit left join T_his_sm on T_his_submit.n_oldid = T_his_sm.n_oldid)left join t_submit_report on T_his_submit.s_id_FromMOID= t_submit_report.s_id_FromMOID)left join t_errsm on T_his_submit.n_oldid = t_errsm.n_oldid order by d_date desc"
//strsql= "select * from t_initsm order by d_date desc"
rs.open strsql,conn,1,3
%>
<table class="news_table" cellpadding="0" cellspacing="0">
<thead>
<tr>
<td width="150" height="25">Time</td>
<td width="100">MoblieCode</td>
<td width="200">SM</td>
<td width="233">Worked</td>
<td width="169">The State的 of Sending</td>
</tr>
</thead>
<tbody>
<%
//---获取结构填充表格---
if rs.bof and rs.eof then
response.Write("no record")
else
while not rs.eof
response.Write("<tr>")
response.Write("<td>"&rs("d_date") &"</td>")
response.Write("<td>"&rs("s_mobilecode")&"</td>")
response.Write("<td>"&rs("s_sm")&"</td>")
response.Write("<td>"&rs("s_MsgContent")& "</td>")
response.Write("<td>"&rs("n_code")& "</td>")
response.Write("</tr>")
rs.movenext
wend
end if
//---获取结构填充表格-
%>
</tbody>
</table>
<%
//为了每次调用这个面可以看到更新的结果集,特意在每次调用本页时添加一条记录
//rs.addNew
//rs("title")=now()&"--title"
//rs("ower")=request.QueryString("timestamp")
//rs("content")="--content"&now()
//rs.update
//释放资源
rs.close
set rs=nothing
conn.close
set conn=nothing
%>

</body>
</html>




...全文
110 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
myvicy 2008-03-07
怎会不行?少了换行你自己加一下.
回复
无线空虚EMS 2008-03-07
想法是不错的,但是具体怎么实现。我按照你的意思做了,可能是好像不行。

你可以不可以把修改后的代码贴出来呢?谢谢啊
回复
把你的所有要输出的信息都赋给一个变量
然后用escape(变量)转换下,在显示页再用unescape转换输出
回复
后台用这个形式:response.Write(escape(rs("laiyuan")))
前台用这个输出:unescape(xmlhttp.responseText);
注意红色部分
回复
无线空虚EMS 2008-03-07
不行哦,朋友~~·
回复
myvicy 2008-03-07

ASP代码:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Session.CodePage=936%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> 无标题文档 </title>

</head>


<body>
<%

dim conn
dim connstr
on error resume next
connstr="Driver={SQL Server};Server=192.168.0.101;UID=sa;PWD=mjt_0755;Database=GQBD"
set conn=server.createobject("ADODB.CONNECTION")
set Rs=Server.CreateObject("ADODB.RecordSet")
conn.open connstr
//sql="select CONVERT(varchar(12) , T_his_submit.d_logdate, 102 ) as d_date , s_mobilecode,(t_his_sm.s_sm)as s_sm,s_MsgContent from T_his_submit left join t_his_sm on T_his_submit.n_oldid = t_his_sm.n_oldid order by T_his_submit.d_logdate desc"
strsql = "select CONVERT(varchar(12) , T_his_submit.d_logdate, 102 ) as d_date , t_his_sm.s_mobilecode,t_his_sm.s_sm,s_MsgContent,n_code from ((T_his_submit left join T_his_sm on T_his_submit.n_oldid = T_his_sm.n_oldid)left join t_submit_report on T_his_submit.s_id_FromMOID= t_submit_report.s_id_FromMOID)left join t_errsm on T_his_submit.n_oldid = t_errsm.n_oldid order by d_date desc"
//strsql= "select * from t_initsm order by d_date desc"
rs.open strsql,conn,1,3
%>
<table class="news_table" cellpadding="0" cellspacing="0">
<thead>
<tr>
<td width="150" height="25"> Time </td>
<td width="100"> MoblieCode </td>
<td width="200"> SM </td>
<td width="233"> Worked </td>
<td width="169"> The State的 of Sending </td>
</tr>
</thead>
<tbody>
<%
//---获取结构填充表格---
response.charset="gb2312"if rs.bof and rs.eof then
response.Write("no record")
else
while not rs.eof
response.Write(" <tr> ")
response.Write(" <td> "&rs("d_date") &" </td> ")
response.Write(" <td> "&rs("s_mobilecode")&" </td> ")
response.Write(" <td> "&rs("s_sm")&" </td> ")
response.Write(" <td> "&rs("s_MsgContent")& " </td> ")
response.Write(" <td> "&rs("n_code")& " </td> ")
response.Write(" </tr> ")
rs.movenext
wend
end if
//---获取结构填充表格-
%>
</tbody>
</table>
<%
//为了每次调用这个面可以看到更新的结果集,特意在每次调用本页时添加一条记录
//rs.addNew
//rs("title")=now()&"--title"
//rs("ower")=request.QueryString("timestamp")
//rs("content")="--content"&now()
//rs.update
//释放资源
rs.close
set rs=nothing
conn.close
set conn=nothing
%>

</body>
</html>
回复
相关推荐
发帖
CSS
创建于2007-09-28

6.0w+

社区成员

层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。
申请成为版主
帖子事件
创建了帖子
2008-03-07 09:45
社区公告
暂无公告