function createXmlHttpRequest()
{
if(window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
if(window.XMLHttpRequest)
return new XMLHttpRequest();
}
var xmlHttpRequest = createXmlHttpRequest();
function openWindow(windowDiv)
{
xmlHttpRequest.onreadystatechange= function()
{
if(xmlHttpRequest.readyState==4 &&xmlHttpRequest.status==200)
{
var response = xmlHttpRequest.responseText;
alert(response);
windowDiv.innerHTML=response;
response=null;
url=null;
windowDiv=null;
}
}
//xmlHttpRequest.open("post",url,true);
//xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//xmlHttpRequest.send("name=在&password=12346");
xmlHttpRequest.open("get","test.html",true);
xmlHttpRequest.send();
}
我整个项目都是用GBK编码,如果test.html中有中文就会出现乱码,求救!!!
{
if(window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
if(window.XMLHttpRequest)
return new XMLHttpRequest();
}
var xmlHttpRequest = createXmlHttpRequest();
function openWindow(windowDiv)
{
xmlHttpRequest.onreadystatechange= function()
{
if(xmlHttpRequest.readyState==4 &&xmlHttpRequest.status==200)
{
var response = xmlHttpRequest.responseText;
alert(response);
windowDiv.innerHTML=response;
response=null;
url=null;
windowDiv=null;
}
}
//xmlHttpRequest.open("post",url,true);
//xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//xmlHttpRequest.send("name=在&password=12346");
xmlHttpRequest.open("get","test.html",true);
xmlHttpRequest.send();
}
我整个项目都是用GBK编码,如果test.html中有中文就会出现乱码,求救!!!
/// <summary>
/// 对中文进行编码
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private string escape(string value)
{
StringBuilder sb = new StringBuilder();
byte[] ba = System.Text.Encoding.Unicode.GetBytes(value);
for (int i = 0; i < ba.Length; i += 2)
{
sb.Append("%u");
sb.Append(ba[i + 1].ToString("X2"));
sb.Append(ba[i].ToString("X2"));
}
return sb.ToString();
}
在后台进行编码处理
前台的时候用unescape进行解码吧,这样不会出现乱码问题
这个功能我大概描述下,当面页面有一个DIV,通过回调var response = xmlHttpRequest.responseText;就可以获得test.html页的代码,原后windowDiv.innerHTML=response; 放进DIV里去,test.html有中文就出现乱码了。
解决编码问题,一般返回字节流即可,然后转换,可以用responseBody属性
index.html
<html>
<head>
<title>AjaxTest</title>
<script>
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function startRequest()
{
createXMLHttpRequest();
try
{
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "data.asp", true);
xmlHttp.setRequestHeader("If-Modified-Since","0");
xmlHttp.send(null);
}
catch(exception)
{
alert("xmlHttp Fail");
}
}
function handleStateChange()
{
if(xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200 || xmlHttp.status == 0)
{
alert(bytes2BSTR(xmlHttp.responseBody));
}
}
}
</script>
<script language="vbscript">
Function bytes2BSTR(vIn)
Dim strReturn,i,ThisCharCode,innerCode,Hight8,Low8,NextCharCode
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>
</head>
<body>
<div>
<input type="button" value="AjaxTest" onclick="startRequest();" />
</div>
</body>
</html>data.asp
<%
Response.Write("你好")
Response.End
%>