当前位置: 首页 > 网络学院 > 客户端脚本教程 > AJAX > AJAX ResponseXML

AJAX
AJAX 实例
AJAX浏览器
AJAX源代码
AJAX服务端
AJAX 数据库
AJAX与XML文件
AJAX XMLHttpRequest
微软的Ajax
AJAX 介绍
AJAX HTTP 请求
AJAX 服务器端脚本
AJAX Suggest 案例
AJAX ResponseXML
AJAX AppML

AJAX ResponseXML


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 822 ::
收藏到网摘: n/a

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或其它程序语言也可以非常方便地对它进行重写。请看相应的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

登陆 | 还没注册?