ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: AJAX :: AJAXÓëXMLÎļþ

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

AJAX
AJAX ʵÀý
AJAXä¯ÀÀÆ÷
AJAXÔ´´úÂë
AJAX·þÎñ¶Ë
AJAX Êý¾Ý¿â
AJAXÓëXMLÎļþ
AJAX XMLHttpRequ..
΢ÈíµÄAjax
AJAX ½éÉÜ
AJAX HTTP ÇëÇó
AJAX ·þÎñÆ÷¶Ë½Å±..
AJAX Suggest °¸À..
AJAX ResponseXML
AJAX AppML

AJAXÓëXMLÎļþ


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ä¯ÀÀ:5625 :: ::

AJAX can be used for interactive communication with an XML file.
AJAX¿ÉÒÔͨ¹ýʹÓÃXMLÎļþÀ´ÈÃÐÅÏ¢²úÉú»¥¶¯


AJAX XML ʵÀý

In the AJAX example below we will demonstrate how a web page can fetch information from an XML file using AJAX technology.
ÔÚÏÈÃæµÄAJAXʵÀýÖÐÎÒÃǽ«ÑÝʾÈçºÎÈÃWEBÒ³ÃæÊ¹ÓÃAJAX¼¼ÊõÀ´»ñÈ¡µ½À´×ÔXMLÎļþµÄÐÅÏ¢


´ÓÏÂÀ­¿òÖÐÑ¡ÔñÒ»ÅÌCD

Ñ¡ÔñCD:
CDµÄÐÅÏ¢¾Í»áÁÐÔÚÏÂÃæ¡£

AJAX ʵÀý½âÎö

The example above contains a simple HTML form and a link to a JavaScript:
ÉÏÃæµÄ¾ÙÀý°üº¬Á˼òµ¥µÄHTML±íµ¥ÒÔ¼°Á¬½Óµ½JSµÄlink£º

<html>
<head>
<script src="selectcd.js"></script>
</head>
<body>
<form> 
Select a CD:
<select name="cds" onchange="showCD(this.value)">
<option value="Bob Dylan">Bob Dylan</option>
<option value="Bonnie Tyler">Bonnie Tyler</option>

<option value="Dolly Parton">Dolly Parton</option>
</select>
</form>
<p>
<div id="txtHint"><b>CD info will be listed here.</b></div>

</p>
</body>
</html>

As you can see it is just a simple HTML form  with a simple drop down box called "cds".
ÕýÈçÄãËù¿´µ½µÄ£¬ËüÖ»ÊǼòµ¥µÄHTML±íµ¥£¬ÀïÃæÓиöÃûΪ"cds"µÄÏÂÀ­¿ò

The paragraph below the form contains a div called "txtHint". The div is used as a placeholder for info retrieved from the web server.
ÔÚ±íµ¥ÏÂÃæµÄ¶ÎÂäÀïÓÐÒ»¸öÃûΪ"txtHint"µÄdiv¡£Ëü¿ÉÓÃÀ´ÏÔʾ´Óweb·þÎñÆ÷ÉÏ»ñÈ¡µ½µÄÐÅÏ¢

When the user selects data, a function called "showCD" is executed. The execution of the function is triggered by the "onchange" event. In other words: Each time the user change the value in the drop down box, the function showCD is called.
µ±Óû§Ñ¡ÔñÁËÐÅÏ¢£¬Ò»¸öÃûΪ"showCD"µÄº¯Êý¾Í»á±»Ö´ÐС£Õâ¸öº¯ÊýÖ´ÐÐÓë"onchange"ʼþÏà¹ØÁª¡£»»¾ä»°Ëµ£ºÃ¿µ±Óû§¸Ä±äÁËÏÂÀ­¿òÀïµÄÄÚÈÝ£¬Õâ¸öº¯Êý¾Í»áÖ´ÐС£

The JavaScript code is listed below.
JS´úÂ뽫ÔÚÏÂÃæÁгö


The AJAX JavaScript

This is the JavaScript code stored in the file "selectcd.js":
Õâ¸öÃûΪ"selectcd.js"µÄJSÎļþ±£´æÁËÎÒÃÇÇ°ÃæËù½²µ½µÄ´úÂ룺

var xmlHttp

function showCD(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getcd.asp"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=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()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}


AJAX ·þÎñ¶ËÒ³Ãæ

The server paged called by the JavaScript, is a simple ASP file called "getcd.asp".
±»JSËùµ÷ÓõķþÎñ¶ËÒ³ÃæÃûΪ"getcd.asp"¡£

The page is written in VBScript for an Internet Information Server (IIS). It could easily be rewritten in PHP, or some other server language.
Õâ¸öÒ³ÃæÊÇÓÃVBScriptдµÄ£¬¿ÉÔËÐÐÔÚIISÉÏ¡£Ëü¿ÉÒԷdz£ÈÝÒ×µÄд³ÉÆäËû·þÎñ¶Ë½Å±¾ÓïÑÔ¡£

The code runs a query against an XML file and returns the result as HTML:
´úÂë»á¶ÔXMLÎļþ½øÐвéѯ£¬²¢½«½á¹û·µ»Øµ½ÒÔHTMLµÄÐÎʽ·µ»Ø£º

q=request.querystring("q")

set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load(Server.MapPath("cd_catalog.xml"))

set nodes=xmlDoc.selectNodes("CATALOG/CD[ARTIST='" & q & "']")

for each x in nodes
for each y in x.childnodes
response.write("<b>" & y.nodename & ":</b> ")
response.write(y.text)
response.write("<br />")
next
next

ÆÀÂÛ (3) 1 All