第一次接触ajax碰到的乱码问题

keaizhong 2006-05-16 11:33:03
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>

<script language="javascript" type="text/javascript">
var request = false;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}

if (!request)
alert("Error initializing XMLHttpRequest!");

function getCustomerInfo() {
var phone = document.getElementById("phone").value;
var url = "http://192.168.1.223/ajax/test.php?phone=" + escape(phone);
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
function getSalesData() {
var url = "http://192.168.1.223/ajax/test.php";
request.open("HEAD", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
function postCustomerInfo() {
var key = document.getElementById("key").value;
var submit1 = document.getElementById("submit1").value;
var page = document.getElementById("page").value;
var url = "http://www.0577hr.com/search_.php";
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gb2312");
request.onreadystatechange = updatePage;
var postUrl = "key=" + escape(key) + "&submit1=" + escape(submit1) +"&page=" + escape(page);
request.send(postUrl);
}

function updatePage() {
if (request.readyState == 4){
//alert(request.responseText);
var html1 = unescape(request.responseText);//这里输出是乱码
      //这里想加个html清空,不知道用什么?
html.document.write( html1 );
}
}

function loading(){
html.document.write( "Loading..." );
}
</script>

<table border=0 cellspacing=1 cellpadding=1 bgcolo=eeeeee>
<tr>
<form name="pagesee" method="post">
<input type="hidden" id="key" value="where pubdate >= NOW() - INTERVAL 30 DAY && i_state != 2 && c_state !=2 ">
<input type="hidden" id="submit1" value="">
<td colspan=5 align=right><input type="text" id="page" value="1" size=5>
<input type=button onclick='loading();page.value--;postCustomerInfo()' value='上一页' > |
<input type=button onclick='loading();page.value++;postCustomerInfo()' value='下一页' >
</td>
</form>
</tr>
</table>
<iframe id="html" name="html" src="" width="100%" height="400"></iframe>
</BODY>
</HTML>

问题出在哪里??
...全文
241 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
keaizhong 2006-05-19
  • 打赏
  • 举报
回复
最后成这样子就对了。哈哈。。多谢楼上的每一位。。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<script language="javascript" type="text/javascript">

var request = false;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}

if (!request)
alert("Error initializing XMLHttpRequest!");

function getCustomerInfo() {
var phone = document.getElementById("phone").value;
var url = "http://192.168.1.223/ajax/test.php?phone=" + escape(phone);
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
function getSalesData() {
var url = "http://192.168.1.223/ajax/test.php";
request.open("HEAD", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
function postCustomerInfo() {
var key = document.getElementById("key").value;
var submit1 = document.getElementById("submit1").value;
var page = document.getElementById("page").value;
var url = "http://www.0577hr.com/search_.php";
var postUrl = "key=" + key + "&submit1=" + submit1 +"&page=" + page;

request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.onreadystatechange = updatePage;
request.send(postUrl);
}

function updatePage() {
if (request.readyState == 4){
//alert(request.responseText);
var html1 = bytes2BSTR(request.responseBody);
html.document.open();
html.document.write( html1 );
html.document.close();
}
}

function loading(){
html.document.open();
html.document.write( "Loading..." );
html.document.close();
}
</script>

<table border=0 cellspacing=1 cellpadding=1 bgcolo=eeeeee>
<tr>
<form name="pagesee" method="post">
<input type="hidden" id="key" value="where pubdate >= NOW() - INTERVAL 3 DAY && i_state != 2 && c_state !=2 ">
<input type="hidden" id="submit1" value="">
<td colspan=5 align=right><input type="text" id="page" value="1" size=5>
<input type=button onclick='loading();page.value--;postCustomerInfo();' value='PREV'> |
<input type=button onclick='loading();page.value++;postCustomerInfo();' value='NEXT'>
</td>
</form>
</tr>
</table>
<iframe id="html" name="html" src="" width="100%" height="400"></iframe>
<script language="vbs">
Function bytes2BSTR(vIn)
Dim strReturn()
j=LenB(vIn)
ReDim strReturn(j+1)
For i = 1 To j
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn(i) = Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn(i) = Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = Join(strReturn,"")
End Function

</script>
</BODY>
</HTML>
keaizhong 2006-05-19
  • 打赏
  • 举报
回复
go_rush不好意思,之前我没有用上,是因为responseText,现在我改用了responseBody
keaizhong 2006-05-19
  • 打赏
  • 举报
回复
谢谢闪电回归,我要的就是这个东东。哈哈。。
玄之丞 2006-05-19
  • 打赏
  • 举报
回复
速度上差好幾級..
玄之丞 2006-05-19
  • 打赏
  • 举报
回复
to ice_berg16(寻梦的稻草人)

用數組,不要用那個字符串

Function bytes2BSTR(vIn)
Dim strReturn()
j=LenB(vIn)
ReDim strReturn(j+1)
For i = 1 To j
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn(i) = Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn(i) = Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = Join(strReturn,"")
End Function
ice_berg16 2006-05-19
  • 打赏
  • 举报
回复
1.不需要escape和unescape。
2.如果服务器你不能修改代码,那就需要进行编码转换,这个只能用VBS做到
网上现在的代码
Function bytes2BSTR(vIn)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
</script>
Go_Rush 2006-05-18
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/4748/4748452.xml?temp=.6224329

这个帖子得到的难道不是中文,你alert出来看看
gu1dai 2006-05-18
  • 打赏
  • 举报
回复
var html1 = unescape(request.responseText);//这里输出是乱码


为什么要unescape?


不能unescape,你可以用vbs将gbk转为utf8

.由于服务器是别人的,所以你只能这么做。
keaizhong 2006-05-18
  • 打赏
  • 举报
回复
不得已再顶一下,希望有个好的解答。。。
lightninga 2006-05-18
  • 打赏
  • 举报
回复
关注
keaizhong 2006-05-18
  • 打赏
  • 举报
回复
unescape是我在网上查的,说加上去不会乱码,但我加了还是乱,我去掉也乱。
request.responseText回来的不是utf-8的编码吗??我想有没有用js转换utf-8成gb2312的。

Go_rush,如果我没有看错的话,你是取得中文部分再来转换的。
我这个是整个页面啊,是不是不同?
我直接COPY那个VBS的函数和reCode()来用,会出错,不知道有什么要求?
Javascrip基础比较差,如果能讲解一下就太感谢了。
Go_Rush 2006-05-17
  • 打赏
  • 举报
回复
当然有时候,为了达到同一个目的。

要想办法避开中文问题。中文字符。

比如这个帖子
http://community.csdn.net/Expert/topic/4751/4751359.xml?temp=.3371546
相对于
http://community.csdn.net/Expert/topic/4748/4748452.xml?temp=.6224329

巧妙避开了对中文问题的处理,改进后程序速度效率提高了几个数量级
Go_Rush 2006-05-17
  • 打赏
  • 举报
回复
你看看这个帖子

http://community.csdn.net/Expert/topic/4748/4748452.xml?temp=.6224329

看他是怎么处理中文乱码的。还有最后一个回复,对处理中文问题的一个改进,值得一看
keaizhong 2006-05-17
  • 打赏
  • 举报
回复
不好意思,自己转到JS版来了。
大家看看得到的页面信息编码是utf-8的,显示为乱码,能否用js来改变其编码方式,不会显示乱码?
keaizhong 2006-05-17
  • 打赏
  • 举报
回复
好像和阿舜的问题不一样的哟。。
你运行一下我那个,然后翻页看看。
得到的是一串乱码呀。我就想得到可以看的中文资料。。
keaizhong 2006-05-16
  • 打赏
  • 举报
回复
我只要得到对方页面,并正确显示就OK了。
找到了一个JS的编码转换,好像没用。你们都是怎么整的这个?
keaizhong 2006-05-16
  • 打赏
  • 举报
回复
PHP页面是别人的,无法控制。。
查了很多资料说要用escape一下,
再用unescape回来就不会乱了,我的还是会乱。。

有没有可以直接在我上面贴出的代码里控制编码的?
ice_berg16 2006-05-16
  • 打赏
  • 举报
回复
xmlhttp的编码是utf8,你页面的编码最好为utf8,
如果不是,那么返回的字串要指定编码
php中用
header("Content-Type:text/html;charset=GB2312");

另外不需要使用escape
使用encodeURIComponent("中文");就可以了,服务器端会自动解码
接收时也不必用unescape

87,910

社区成员

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

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