⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ajax_responsexml.asp@output=print

📁 W3Schools tutorial..web designing
💻 ASP@OUTPUT=PRINT
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>AJAX ResponseXML Example</title>
 
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />

<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

<script src="selectcustomer_xml.js" type="text/javascript"></script> 
</head>
<body>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

<h1>AJAX ResponseXML Example</h1>
<a href="ajax_xmlfile.asp"><img border="0" src="../images/btn_previous.gif" width="100" height="20" alt="Previous" /></a>
<a href="ajax_examples.asp"><img border="0" src="../images/btn_next.gif" width="100" height="20" alt="Next" /></a>
<hr />

<p class="intro">While responseText returns the HTTP response as a string, 
responseXML returns the response as XML.</p>
<p class="intro">The ResponseXML property returns an XML document object, which 
can be examined and parsed using W3C DOM node tree methods and properties.</p>
<hr />
<h2>AJAX ResponseXML Example</h2>
<p>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.</p>
<hr />
<h2>Select a Name in the Box Below</h2>
<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><br />
<span id="city"></span><br />
<span id="country"></span>
<hr />

<h2>AJAX Example Explained</h2>
<p>The example above contains an HTML form, several &lt;span&gt; elements to hold the 
returned data, and a link to a JavaScript:</p>

<table class="ex" id="table6" border="1" width="100%">
	<tr>
		<td>
		<pre>&lt;html&gt;
&lt;head&gt;
&lt;script src=&quot;selectcustomer_xml.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;</pre>
		<pre>&lt;form action=&quot;&quot;&gt; 
Select a Customer:
&lt;select name=&quot;customers&quot; onchange=&quot;showCustomer(this.value)&quot;&gt;
&lt;option value=&quot;ALFKI&quot;&gt;Alfreds Futterkiste&lt;/option&gt;
&lt;option value=&quot;NORTS &quot;&gt;North/South&lt;/option&gt;
&lt;option value=&quot;WOLZA&quot;&gt;Wolski Zajazd&lt;/option&gt;
&lt;/select&gt;
&lt;/form&gt;</pre>
		<pre>&lt;b&gt;&lt;span id=&quot;companyname&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span id=&quot;contactname&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;address&quot;&gt;&lt;/span&gt;
&lt;span id=&quot;city&quot;&gt;&lt;/span&gt;&lt;br/&gt;
&lt;span id=&quot;country&quot;&gt;&lt;/span&gt;</pre>
		<pre>&lt;/body&gt;
&lt;/html&gt;</pre>
		</td>
	</tr>
</table>
<p>The example above contains an HTML form with a drop down box called 
&quot;customers&quot;.</p>
<p>When the user selects a customer in the dropdown box, a function called &quot;showCustomer()&quot; is executed. The 
execution of the function is triggered by the &quot;onchange&quot; event. In other words: 
Each time the user change the value in the drop down box, the function showCustomer() is called.</p> 

<p>
The JavaScript code is listed below.</p>
<hr />
<h2>The AJAX JavaScript</h2>
<p>This is the JavaScript code stored in the file &quot;selectcustomer_xml.js&quot;:</p>
<table class="ex" id="table7" border="1" width="100%">
	<tr>
		<td>
		<pre>var xmlHttp</pre>
		<pre>function showCustomer(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert (&quot;Your browser does not support AJAX!&quot;);
  return;
  }
var url=&quot;getcustomer_xml.asp&quot;;
url=url+&quot;?q=&quot;+str;
url=url+&quot;&amp;sid=&quot;+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open(&quot;GET&quot;,url,true);
xmlHttp.send(null);
}</pre>
		<pre>function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{
var xmlDoc=xmlHttp.responseXML.documentElement;
document.getElementById(&quot;companyname&quot;).innerHTML=
xmlDoc.getElementsByTagName(&quot;compname&quot;)[0].childNodes[0].nodeValue;
document.getElementById(&quot;contactname&quot;).innerHTML=
xmlDoc.getElementsByTagName(&quot;contname&quot;)[0].childNodes[0].nodeValue;
document.getElementById(&quot;address&quot;).innerHTML=
xmlDoc.getElementsByTagName(&quot;address&quot;)[0].childNodes[0].nodeValue;
document.getElementById(&quot;city&quot;).innerHTML=
xmlDoc.getElementsByTagName(&quot;city&quot;)[0].childNodes[0].nodeValue;
document.getElementById(&quot;country&quot;).innerHTML=
xmlDoc.getElementsByTagName(&quot;country&quot;)[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(&quot;Msxml2.XMLHTTP&quot;);
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
    }
  }
return xmlHttp;
}</pre>
		</td>
	</tr>
</table>
<p>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.</p><hr />
<h2>The AJAX Server Page</h2>
<p>The server page called by the JavaScript, is a simple ASP file called 
&quot;getcustomer_xml.asp&quot;.</p>
<p>The page is written in VBScript for an Internet Information Server (IIS). It 
could easily be rewritten in PHP, or some other server language.
<a href="../php/php_ajax_responsexml.asp">Look at a corresponding example in PHP</a>.</p>
<p>The code runs an SQL query against a database and returns the result as an XML 
document:</p>
<table class="ex" id="table8" border="1" width="100%">
	<tr>
		<td>
		<pre>&lt;%
response.expires=-1
response.contenttype=&quot;text/xml&quot;</pre>
		<pre>sql=&quot;SELECT * FROM CUSTOMERS &quot;
sql=sql &amp; &quot; WHERE CUSTOMERID='&quot; &amp; request.querystring(&quot;q&quot;) &amp; &quot;'&quot;

on error resume next
set conn=Server.CreateObject(&quot;ADODB.Connection&quot;)
conn.Provider=&quot;Microsoft.Jet.OLEDB.4.0&quot;
conn.Open(Server.Mappath(&quot;/db/northwind.mdb&quot;))
set rs=Server.CreateObject(&quot;ADODB.recordset&quot;)
rs.Open sql, conn</pre>
		<pre>if err &lt;&gt; 0 then
response.write(err.description)
set rs=nothing
set conn=nothing
else
response.write(&quot;&lt;?xml version='1.0' encoding='ISO-8859-1'?&gt;&quot;)
response.write(&quot;&lt;company&gt;&quot;)
response.write(&quot;&lt;compname&gt;&quot; &amp;rs.fields(&quot;companyname&quot;)&amp; &quot;&lt;/compname&gt;&quot;)
response.write(&quot;&lt;contname&gt;&quot; &amp;rs.fields(&quot;contactname&quot;)&amp; &quot;&lt;/contname&gt;&quot;)
response.write(&quot;&lt;address&gt;&quot; &amp;rs.fields(&quot;address&quot;)&amp; &quot;&lt;/address&gt;&quot;)
response.write(&quot;&lt;city&gt;&quot; &amp;rs.fields(&quot;city&quot;)&amp; &quot;&lt;/city&gt;&quot;)
response.write(&quot;&lt;country&gt;&quot; &amp;rs.fields(&quot;country&quot;)&amp; &quot;&lt;/country&gt;&quot;)
response.write(&quot;&lt;/company&gt;&quot;)
end if
on error goto 0
%&gt;</pre>
		</td>
	</tr>
</table>
<p>Notice the second line in the ASP code above: response.contenttype=&quot;text/xml&quot;. 
The ContentType property sets the HTTP content type for the response object. The 
default value for this property is &quot;text/html&quot;. This time we want the content 
type to be XML.</p>
<p>Then we select data from the database, and builds an XML document with the 
data. </p>
<hr />

<a href="ajax_xmlfile.asp"><img border="0" src="../images/btn_previous.gif" width="100" height="20" alt="Previous" /></a>
<a href="ajax_examples.asp"><img border="0" src="../images/btn_next.gif" width="100" height="20" alt="Next" /></a>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -