w3pop.com :: ÍøÂçѧԺ :: AJAX :: AJAXÓëXMLÎļþ
AJAX can be used for interactive communication with an XML file.
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ÎļþµÄÐÅÏ¢
The example above contains a simple HTML form and a link to a JavaScript:
ÉÏÃæµÄ¾ÙÀý°üº¬Á˼òµ¥µÄHTML±íµ¥ÒÔ¼°Á¬½Óµ½JSµÄlink£º
<html> <body> <form> <p> </body> |
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´úÂ뽫ÔÚÏÂÃæÁгö
This is the JavaScript code stored in the file "selectcd.js":
Õâ¸öÃûΪ"selectcd.js"µÄJSÎļþ±£´æÁËÎÒÃÇÇ°ÃæËù½²µ½µÄ´úÂ룺
var xmlHttp |
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")
|