示例一:該示例返回每個Node 的value.
<html>
<body>
<script type="text/vbscript">
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
for each x in xmlDoc.documentElement.childNodes
?document.write(x.childnodes(0).nodeValue & "<br />")
next
</script>
</body>
</html
準備一個note.xml文件,很簡單幾行:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
<note time="12:03:46">
? <to>Tove</to>
? <from>Jani</from>
? <heading>Reminder</heading>
? <body>Don't forget me this weekend!</body>
</note>
示例二:創(chuàng)建一個XMLHttpRequest.
<html>
<head>
<script type="text/javascript">
var xmlhttp
function loadXMLDoc(url)
{
// code for Mozilla, etc.
if (window.XMLHttpRequest)
? {
? xmlhttp=new XMLHttpRequest()
? xmlhttp.onreadystatechange=state_Change
? xmlhttp.open("GET",url,true)
? xmlhttp.send(null)
? }
// code for IE
else if (window.ActiveXObject)
? {
? xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
??? if (xmlhttp)
??? {
??? xmlhttp.onreadystatechange=state_Change
??? xmlhttp.open("GET",url,true)
??? xmlhttp.send()
??? }
? }
}
function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
? {
? // if "OK"
? if (xmlhttp.status==200)
? {
? alert("XML data OK")
? document.getElementById('A1').innerHTML=xmlhttp.status
? document.getElementById('A2').innerHTML=xmlhttp.statusText
? document.getElementById('A3').innerHTML=xmlhttp.responseText
? }
? else
? {
? alert("Problem retrieving XML data:" + xmlhttp.statusText)
? }
? }
}
</script>
</head>
<body script_onload="loadXMLDoc('note.xml')">
<h2>Using the HttpRequest Object</h2>
<p><b>status:</b>
<span id="A1"></span>
</p>
<p><b>status text:</b>
<span id="A2"></span>
</p>
<p><b>response:</b>
<br><span id="A3"></span>
</p>
</body>
</html>
對著代碼 和運行結(jié)果看,很容易熟悉XMLHttpRequest對象.
function loadXMLDoc(url)中根據(jù)不同的客戶端創(chuàng)建XMLHttpRequest對象,然后對傳入的url向Server發(fā)送GET請求,true參數(shù)是為了表明要異步請求.完了之后 ,state_Change()被調(diào)用,該方法就是得到Server返回的狀態(tài).并傳給網(wǎng)頁顯示出來.
posted on 2006-04-04 17:02
kelven 閱讀(331)
評論(0) 編輯 收藏 所屬分類:
Ajax