ÍøÂçѧԺ

w3pop.com :: ÍøÂçѧԺ :: AJAX :: AJAX ResponseXML

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

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

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

AJAX ResponseXML


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-08-24 ä¯ÀÀ:6109 :: ::

While responseText returns the HTTP response as a string, responseXML returns the response as XML.
“responseText”ÊôÐÔÒÔ×Ö·û´®ÐÎʽ·µ»ØHTTPÏìÓ¦£»“responseXML”ÊôÐÔÒÔXMLÐÎʽ·µ»ØHTTPÏìÓ¦¡£

The ResponseXML property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties.
“responseXML”ÊôÐÔ·µ»ØÁËÒ»·ÝXMLÎĵµ¶ÔÏ󣬿ÉÒÔʹÓÃW3C DOM½ÚµãÊ÷·½·¨ºÍÊôÐÔ¶Ô¸ÃXMLÎĵµ¶ÔÏó½øÐмì²éºÍ½âÎö¡£


AJAX ResponseXML Example
AJAX ResponseXML °¸Àý

In the following AJAX example we will demonstrate how a web page can fetch information from a database using AJAX technology. The selected data from the database will this time be converted to an XML document, and then we will use the DOM to extract the values to be displayed.
ÔÚÏÂÊöAJAX°¸ÀýÖУ¬ÎÒÃǽ«¾ßÌå˵Ã÷Ò»ÕÅÍøÒ³ÊÇÈçºÎʹÓÃAJAX¼¼Êõ´ÓÊý¾Ý¿âÖлñÈ¡ÐÅÏ¢µÄ£»´ÓÊý¾Ý¿âÖÐÑ¡ÔñµÄÊý¾Ý½«±»¼´Ê±×ª»»³ÉÒ»·ÝXMLÎĵµ£¬²¢ÇÒ£¬ÎÒÃǽ«Ê¹ÓÃDOM·ÖÀë³öÐèÒªÏÔʾµÄÖµ¡£


Select a Name in the Box Below
ÔÚÏÂÃæµÄÎı¾¿òÖÐÑ¡ÔñÒ»¸öÃû³Æ

Select a Customer:

AJAX Example Explained
AJAX °¸ÀýÆÊÎö

The example above contains an HTML form, several <span> elements to hold the returned data, and a link to a JavaScript:
ÉÏÊö°¸Àý°üº¬ÁËÒ»·ÝHTML±íµ¥£¬²¿·Ö<span>ÔªËØ±£ÁôÁËËù·µ»ØµÄÊý¾ÝÒÔ¼°Ò»¸öÖ¸ÏòJavaScriptµÄÁ´½Ó£º

<html>
<head>
<script src="selectcustomer_xml.js"></script>
</head>
<body>
<form action=""> 
Select a Customer:

<select name="customers" onchange="showCustomer(this.value)">
<option value="ALFKI">Alfreds Futterkiste</option>
<option value="NORTS ">North/South</option>
<option value="WOLZA">Wolski Zajazd</option>

</select>
</form>
<b><span id="companyname"></span></b><br />
<span id="contactname"></span><br />

<span id="address"></span>
<span id="city"></span><br/>
<span id="country"></span>
</body>
</html>

The example above contains an HTML form with a drop down box called "customers".
ÉÏÊö°¸ÀýÁоÙÁËÒ»·Ý°üº¬ÃûΪ“customers [¿Í»§]”ÏÂÀ­²Ëµ¥¿òµÄHTML±íµ¥¡£

When the user selects a customer in the dropdown box, a function called "showCustomer()" 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 showCustomer() is called.
µ±Óû§ÔÚÏÂÀ­²Ëµ¥¿òÖÐÑ¡ÔñÒ»¸ö¿Í»§Ê±£¬½«Ö´ÐГshowCustomer()”º¯Êý¡£¸Ãº¯ÊýµÄÖ´ÐÐÊÇÓÉ“onchange”ʼþ¼¤·¢µÄ£»»»¾ä»°Ëµ£ºÃ¿µ±Óû§¸Ä±äÏÂÀ­²Ëµ¥¿òÖеÄÖµµÄʱºò£¬º¯Êý“showCustomer()”½«±»Ö´ÐС£

The JavaScript code is listed below.
¾ßÌåµÄJavaScript´úÂ뽫ÔÚÏÂÃæÁгö¡£


The AJAX JavaScript
AJAX JavaScript ½Å±¾

This is the JavaScript code stored in the file "selectcustomer_xml.js":
ÏÂÃæÁоÙÁË´¢´æÔÚ“selectcustomer_xml.js”ÎļþÖеÄJavaScript´úÂ룺

var xmlHttp
function showCustomer(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="getcustomer_xml.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)
{
var xmlDoc=xmlHttp.responseXML.documentElement;
document.getElementById("companyname").innerHTML=
xmlDoc.getElementsByTagName("compname")[0].childNodes[0].nodeValue;
document.getElementById("contactname").innerHTML=
xmlDoc.getElementsByTagName("contname")[0].childNodes[0].nodeValue;
document.getElementById("address").innerHTML=
xmlDoc.getElementsByTagName("address")[0].childNodes[0].nodeValue;
document.getElementById("city").innerHTML=
xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
document.getElementById("country").innerHTML=
xmlDoc.getElementsByTagName("country")[0].childNodes[0].nodeValue;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

The showCustomer() and GetXmlHttpObject() functions above are the same as in previous chapters. The stateChanged() function is also used earlier in this tutorial, however; this time we return the result as an XML document (with responseXML) and uses the DOM to extract the values we want to be displayed.
ÉÏÊöµÄ“showCustomer()”º¯ÊýºÍ“GetXmlHttpObject()”º¯ÊýÓëÉÏÒ»ÕÂÎÒÃÇÌáµ½¹ýµÄÏàͬ¡£ÔÚÕâ·Ý½Ì³ÌµÄÇ°Ãæ²¿·ÖÖУ¬ÎÒÃÇ»¹Ê¹ÓÃÁË“stateChanged()”º¯Êý¡£È»¶ø£¬Õâ´ÎÎÒÃÇʹÓÓresponseXML”ÊôÐÔ·µ»ØÁËÒ»·ÝXMLÎĵµ£¬²¢ÇÒ£¬ÎÒÃǽ«Ê¹ÓÃDOM·ÖÀë³öÐèÒªÏÔʾµÄÖµ¡£


The AJAX Server Page
AJAX ·þÎñÆ÷Ò³Ãæ

The server page called by the JavaScript, is a simple ASP file called "getcustomer_xml.asp".
Õâ·Ý±»JavaScriptÇëÇóµÄ·þÎñÆ÷Ò³ÃæÊÇÒ»·ÝÃûΪ“getcustomer_xml.asp”µÄ¼òµ¥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. Look at a corresponding example in PHP.
¸ÃÒ³ÃæÊÇÓÉVBScriptÊéдµÄ£¬²¢ÇÒÖ§³ÖÍøÂçÐÅÏ¢·þÎñÆ÷£¨iis£©¡£Ê¹ÓÃPHP»òÆäËü³ÌÐòÓïÑÔÒ²¿ÉÒԷdz£·½±ãµØ¶ÔËü½øÐÐÖØÐ´¡£Çë¿´ÏàÓ¦µÄPHP°¸Àý¡£

The code runs an SQL query against a database and returns the result as an XML document:
ÏÂÃæµÄ´úÂëÔËÐÐÁËÓÃÓÚ²éѯÊý¾Ý¿âÊý¾ÝµÄSQL²éѯÓï¾ä²¢ÇÒ·µ»ØÁËÒ»·ÝXMLÎĵµ£º

<%
response.expires=-1
response.contenttype="text/xml"
sql="SELECT * FROM CUSTOMERS "
sql=sql & " WHERE CUSTOMERID='" & request.querystring("q") & "'"

on error resume next
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
if err <> 0 then
response.write(err.description)
set rs=nothing
set conn=nothing
else
response.write("<?xml version='1.0' encoding='ISO-8859-1'?>")
response.write("<company>")
response.write("<compname>" &rs.fields("companyname")& "</compname>")
response.write("<contname>" &rs.fields("contactname")& "</contname>")
response.write("<address>" &rs.fields("address")& "</address>")
response.write("<city>" &rs.fields("city")& "</city>")
response.write("<country>" &rs.fields("country")& "</country>")
response.write("</company>")
end if
on error goto 0
%>

Notice the second line in the ASP code above: response.contenttype="text/xml". The ContentType property sets the HTTP content type for the response object. The default value for this property is "text/html". This time we want the content type to be XML.
Çë×¢ÒâÉÏÊöASP´úÂëµÄµÚ¶þÐУºresponse.contenttype="text/xml"¡£“ContentType [ÄÚÈÝÀàÐÍ]”ÊôÐÔΪ“response”ÊôÐÔÉèÖÃÁËHTTPÄÚÈÝÀàÐÍ¡£¸ÃÊôÐÔµÄĬÈÏֵΪ“text/html”¡£ÕâÒ»´Î£¬ÎÒÃÇÏ£Íû½«ËüÉèÖóÉXML¡£

Then we select data from the database, and builds an XML document with the data.
È»ºó£¬ÎÒÃǽ«´ÓÊý¾Ý¿âÖÐÑ¡ÔñÊý¾Ý£¬²¢ÇÒ´´½¨Ò»·Ý°üº¬¾ßÌåÊý¾ÝµÄXMLÎĵµ¡£

ÆÀÂÛ (0) All