DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> AJAX源碼應用示例2-解決AJAX返回中文亂碼問題
AJAX源碼應用示例2-解決AJAX返回中文亂碼問題
編輯:AJAX詳解     

本示例經過本人修改getcustomer.ASP源代碼,最終解決了最頭疼的AJax返回中文數據出現亂碼的問題..

下面為示例源碼文件:

'1,index.Html

<Html>
<head>
<title>阿裡西西-AJax-應用示例2</title>
<meta http-equiv="Content-Type" content="text/Html; charset=gb2312" />
<script src="selectcustomer.JS"></script>
</head><body><form>
請選擇用戶:
<select name="customers" >
<option value="1">阿裡西西</option>
<option value="2">80端口</option>
<option value="3">FM</option>
</select>
</form><p>
<div id="txtHint"><b>用戶信息...</b></div>
</p></body>
</Html>

'2,getcustomer.ASP

<%
sql="SELECT * FROM CUSTOMERS WHERE CUSTOMERID="
sql=sql & request.querystring("q")

set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("db/northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
rs.Open sql, conn

Response.ContentType = "text/XML"
Response.CharSet = "GB2312"
Dim varCount,varReq,XMLdom
varCount = Request.TotalBytes
varReq = Request.BinaryRead(varCount)
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
 Set xmldom = Server.CreateObject("MSXML2.DOMDocument")
 XMLdom.load varReq
 Response.Write "<?xml version=""1.0"" encoding=""GB2312""?>" & xmldom.documentElement.XML
Else
response.write("<table>")
do until rs.EOF
for each x in rs.FIElds
response.write("<tr><td><b>" & x.name & "</b></td>")
response.write("<td>" & x.value & "</td></tr>")
next
rs.MoveNext
loop
response.write("</table>")
End If
%>

'3,selectcustomer.JS

var XMLHttp

function showCustomer(str)
{
var url="getcustomer.ASP?sid=" + Math.random() + "&q=" + str
xmlHttp=GetXMLHttpObject(stateChanged)
XMLHttp.open("GET", url , true)
XMLHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || XMLHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHtml=XMLHttp.responseText
}
}

function GetXMLHttpObject(handler)
{
var objXMLHttp=null

if (navigator.userAgent.indexOf("Opera")>=0)
{
alert("This example doesn't work in Opera")
return;
}
if (navigator.userAgent.indexOf("MSIE")>=0)
{
var strName="Msxml2.XMLHTTP"

if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
{
strName="Microsoft.XMLHTTP"
}
try
{
objXMLHttp=new ActiveXObject(strName)
objXMLHttp.onreadystatechange=handler
return objXMLHttp
}
catch(e)
{
alert("Error. Scripting for ActiveX might be disabled")
return
}
}
if (navigator.userAgent.indexOf("Mozilla")>=0)
{
objXmlHttp=new XMLHttpRequest()
objXMLHttp.onload=handler
objXMLHttp.onerror=handler
return objXMLHttp
}
}

'4,數據庫格式

庫名:northwind.mdb

表名: Customers

字段:
CustomerID 自動編號
Name 文本
NL 數字
Address

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved