﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-paulwong-文章分类-AJAX</title><link>http://www.blogjava.net/paulwong/category/10812.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 21:11:48 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 21:11:48 GMT</pubDate><ttl>60</ttl><item><title>AJAX 上手篇</title><link>http://www.blogjava.net/paulwong/articles/44684.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Fri, 05 May 2006 14:21:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/articles/44684.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/44684.html</wfw:comment><comments>http://www.blogjava.net/paulwong/articles/44684.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/44684.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/44684.html</trackback:ping><description><![CDATA[
		<h1>AJAX 上手篇<o:p></o:p></h1>
		<o:p>
		</o:p>
		<h2>第一步 – 说声「请」 （又称为「怎么发出 XMLHttpRequest」） <o:p></o:p></h2>
		<p>为了用 JavaScript 对服务器发送 HTTP 要求，你必须先以相关的类别（class）制出实体（instance）。Internet<br />Explorer 首先以 ActiveX 对象方式提供 <code>XMLHTTP</code> 类别，而 Mozilla、Safari及其它浏览器则随后以 <code>XMLHttpRequest</code> 类别支持此 ActiveX 对象中的类别及属性。 <o:p></o:p></p>
		<br />
		<p>因此，如果想跨浏览器，那么可以这么写： </p>
		<p>
				<br />
				<o:p>
				</o:p>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (window.XMLHttpRequest) { </span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);"> Mozilla, Safari, <img src="http://www.blogjava.net/images/dot.gif" /></span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    http_request </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> XMLHttpRequest();<br /><br />} </span>
				<span style="color: rgb(0, 0, 255);">else</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (window.ActiveXObject) { </span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);"> IE</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    http_request </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> ActiveXObject(</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">Microsoft.XMLHTTP</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">);<br /><br />}<br /><br /></span>
		</div>
		<o:p>
		</o:p>
		<p>
				<br />
		</p>
		<p>(由于这段程序仅供说明，所以是采最简方式写出。本文第三步中有另一种我们比较常用的写法。) <o:p></o:p></p>
		<p>有些版本的 Mozilla 浏览器在服务器送回的数据未含 XML mime-type 文件头（header）时会出错。为了避免这个问题，你可以用下列方法覆写服务器传回的档头，以免传回的不是<code>text/xml</code>。</p>
		<p>
				<br />
				<o:p>
				</o:p>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">http_request </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> XMLHttpRequest();<br /><br />http_request.overrideMimeType('text</span>
				<span style="color: rgb(0, 0, 0);">/</span>
				<span style="color: rgb(0, 0, 0);">xml');</span>
		</div>
		<o:p>
		</o:p>
		<p>
				<br />
		</p>
		<p>接下来是要决定服务器传回资料后的处理方式，此时你只要以 <code>onreadystatechange</code> 这个属性指明要处理传回值的<br />JavaScript 函式名称即可，例如：</p>
		<p>
				<br />
				<o:p>
				</o:p>
		</p>
		<p>
				<code>
				</code>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">http_request.onreadystatechange </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> nameOfTheFunction;</span>
		</div>
		<br />
		<o:p>
		</o:p>
		<p>
		</p>
		<p>注意，指定的函式名称后不加括号也没有参数。除了指定函式名称外，你也能用 Javascript 实时定义函式的方法来定一个新的处理函式，如下：</p>
		<p>
				<br />
				<o:p>
				</o:p>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">http_request.onreadystatechange </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">function</span>
				<span style="color: rgb(0, 0, 0);">(){<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);"> 做些事</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">
						<br />};</span>
		</div>
		<o:p>
		</o:p>
		<p>
				<br />
		</p>
		<p>决定处理方式之后你得确实发出 request，此时需叫用 HTTP request 类别的 <code>open()</code> 及 <code>send()</code> 方法，如下：</p>
		<p>
				<br />
				<o:p>
				</o:p>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">http_request.open('GET', 'http:</span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);">www.example.org/some.file', true);</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">http_request.send(</span>
				<span style="color: rgb(0, 0, 255);">null</span>
				<span style="color: rgb(0, 0, 0);">);<br /><br /></span>
		</div>
		<o:p>
		</o:p>
		<ul type="disc">
				<li class="MsoNormal">
						<code>open()</code> 的第一个参数是<br />     HTTP request 的方法，也就是从<br />     GET、POST、HEAD 中择一使用，亦可用你主机上支持的方式。为遵循 HTTP 标准，请记得这些方法都是大写，不然有的浏览器（如 Firefox）或许不会理你。其它 HTTP request 可以支持的方法列表请参考<br />     <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html" target="_blank">W3C 规格书</a> (<i><a target="_blank" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html">http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html</a></i>)。 <o:p></o:p></li>
				<li class="MsoNormal">第二个参数是目标 URL。基于安全考虑，你不能叫用同网域以外的网页。如果网域不同，则叫用<br />     <code>open()</code> 时会出现「权限不足，拒绝存取」那类的错误。通常大伙会犯的错误多为在 domain.tld 网的网站下呼叫 <a target="_blank" href="http://www.domain.tld/">www.domain.tld</a> 中的网页，仅是一点点差别都不行。<br />     <o:p></o:p></li>
				<li class="MsoNormal">第三个参数决定此 request 是否不同步进行，如果设定为<br />     <code>TRUE</code> 则即使服务器尚未传回数据也会继续执行其余的程序，这也就是 AJAX 中第一个 A 代表的意义。 <o:p></o:p></li>
		</ul>
		<p>
				<code>send()</code> 的参数在以 POST 发出 request 时可以是任何想传给服务器的东西，而数据则以查询字符串的方式列出，例如： <o:p></o:p></p>
		<p>
				<code>
				</code> </p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">name</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">value</span>
				<span style="color: rgb(0, 0, 0);">&amp;</span>
				<span style="color: rgb(0, 0, 0);">anothername</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">othervalue</span>
				<span style="color: rgb(0, 0, 0);">&amp;</span>
				<span style="color: rgb(0, 0, 0);">so</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">on</span>
		</div>
		<o:p>
		</o:p>
		<p>不过如果你想要以 POST 方式传送数据，则必须先将 MIME 型态改好，如下： <o:p></o:p></p>
		<pre>
				<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 0);">http_request.setRequestHeader('Content</span>
						<span style="color: rgb(0, 0, 0);">-</span>
						<span style="color: rgb(0, 0, 0);">Type', 'application</span>
						<span style="color: rgb(0, 0, 0);">/</span>
						<span style="color: rgb(0, 0, 0);">x</span>
						<span style="color: rgb(0, 0, 0);">-</span>
						<span style="color: rgb(0, 0, 0);">www</span>
						<span style="color: rgb(0, 0, 0);">-</span>
						<span style="color: rgb(0, 0, 0);">form</span>
						<span style="color: rgb(0, 0, 0);">-</span>
						<span style="color: rgb(0, 0, 0);">urlencoded');<br /><br /></span>
				</div>
				<br />
				<o:p>
				</o:p>
		</pre>
		<p>否则服务器就不会理你传过来的数据了。 <o:p></o:p></p>
		<h2>第二步 – 「就上咩！」（又称为「处理服务器传回的数据」） <o:p></o:p></h2>
		<p>传出 request 时必须提供处理传回值的函式名称。 <o:p></o:p></p>
		<p>
				<code>
				</code>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">http_request.onreadystatechange </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> nameOfTheFunction;</span>
		</div>
		<br />
		<o:p>
		</o:p>
		<p>
		</p>
		<pre>////////////////////////////////////////////////////////////////////<o:p></o:p></pre>
		<pre>//但是，FireFox 对onreadyStateChange没有反应，怎么办，这个方法不能用在<o:p></o:p></pre>
		<pre>//FireFox 中，有没有其它的方法？<o:p></o:p></pre>
		<pre>//      Added by <a target="_blank" href="http://www.besook.com/">www.besook.com</a> 2006-03-19<o:p></o:p></pre>
		<pre>//////////////////////////////////////////////////////////////<o:p></o:p></pre>
		<p>那么来看看这个函式该做些什么。首先，它必须检查 request 目前的状态：如果状态值为 4 代表服务器已经传回所有信息了，便可以开始解析所得信息。</p>
		<p>
				<br />
				<o:p>
				</o:p>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (http_request.readyState </span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">4</span>
				<span style="color: rgb(0, 0, 0);">) {<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);"> 一切 ok, 继续解析</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">
						<br />} </span>
				<span style="color: rgb(0, 0, 255);">else</span>
				<span style="color: rgb(0, 0, 0);"> {<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);"> 还没完成</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">
						<br />}</span>
		</div>
		<o:p>
		</o:p>
		<p>
				<code>
						<br />
				</code>
		</p>
		<p>
				<code>readyState</code> 所有可能的值如下： <o:p></o:p></p>
		<ul type="disc">
				<li class="MsoNormal">0 (还没开始) <o:p></o:p></li>
				<li class="MsoNormal">1 (读取中) <o:p></o:p></li>
				<li class="MsoNormal">2 (已读取) <o:p></o:p></li>
				<li class="MsoNormal">3 (信息交换中) <o:p></o:p></li>
				<li class="MsoNormal">4 (一切完成) <o:p></o:p></li>
		</ul>
		<p>(<a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/readystate_1.asp" target="_blank">资料来源: MSDN</a> (<i><a target="_blank" href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/readystate_1.asp">http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/readystate_1.asp</a></i>))<br /><o:p></o:p></p>
		<p>接下来要检查服务器传回的 HTTP 状态码。所有状态码列表可于 <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html" target="_blank">W3C<br />网站</a> (<i><a target="_blank" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html</a></i>)上查到，但我们要管的是<code>200 OK</code> 这种状态。</p>
		<p>
				<br />
				<o:p>
				</o:p>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (http_request.status </span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">200</span>
				<span style="color: rgb(0, 0, 0);">) {<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);"> 万事具备</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">
						<br />} </span>
				<span style="color: rgb(0, 0, 255);">else</span>
				<span style="color: rgb(0, 0, 0);"> {<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);"> 似乎有点问题，或许服务器传回了 404 (查无此页) 或者 500 (内部错误) 什么的</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">
						<br />}</span>
		</div>
		<o:p>
		</o:p>
		<p>
				<br />
		</p>
		<p>
				<br />
		</p>
		<p>检查传回的 HTTP 状态码后，要怎么处理传回的数据就由你决定了。有两种存取数据的方式： <o:p></o:p></p>
		<ul type="disc">
				<li class="MsoNormal">
						<code>http_request.responseText</code> – 这样会把传回值当字符串用<br />     <o:p></o:p></li>
				<li class="MsoNormal">
						<code>http_request.responseXML</code> – 这样会把传回值视为<br />     <code>XMLDocument</code> 对象，而后可用<br />     JavaScript DOM 相关函式处理 <o:p></o:p></li>
		</ul>
		<h2>第三步 - 万事俱备 - 简单范例 <o:p></o:p></h2>
		<p>好，接着就做一次简单的 HTTP 范例，演示方才的各项技巧。这段 JavaScript 会向服务器要一份里头有「I'm<br />a test.」字样的 HTML 文件(<code>test.html</code>)，而后以 <code>alert()</code> 将文件内容列出。</p>
		<p>
				<br />
				<o:p>
				</o:p>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">&lt;</span>
				<span style="color: rgb(0, 0, 0);">script type</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">text/javascript</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);"> language</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">javascript</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">var</span>
				<span style="color: rgb(0, 0, 0);"> http_request </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">false</span>
				<span style="color: rgb(0, 0, 0);">;<br /><br />    </span>
				<span style="color: rgb(0, 0, 255);">function</span>
				<span style="color: rgb(0, 0, 0);"> makeRequest(url) {<br /><br />        http_request </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">false</span>
				<span style="color: rgb(0, 0, 0);">;<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (window.XMLHttpRequest) { </span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);"> Mozilla, Safari,<img src="http://www.blogjava.net/images/dot.gif" /></span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">
						<br />            http_request </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> XMLHttpRequest();<br /><br />            </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (http_request.overrideMimeType) {<br /><br />                http_request.overrideMimeType('text</span>
				<span style="color: rgb(0, 0, 0);">/</span>
				<span style="color: rgb(0, 0, 0);">xml');<br /><br />            }<br /><br />        } </span>
				<span style="color: rgb(0, 0, 255);">else</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (window.ActiveXObject) { </span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);"> IE</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">
						<br />            </span>
				<span style="color: rgb(0, 0, 255);">try</span>
				<span style="color: rgb(0, 0, 0);"> {<br /><br />                http_request </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> ActiveXObject(</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">Msxml2.XMLHTTP</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">);<br /><br />            } </span>
				<span style="color: rgb(0, 0, 255);">catch</span>
				<span style="color: rgb(0, 0, 0);"> (e) {<br /><br />                </span>
				<span style="color: rgb(0, 0, 255);">try</span>
				<span style="color: rgb(0, 0, 0);"> {<br /><br />                    http_request </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> ActiveXObject(</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">Microsoft.XMLHTTP</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">);<br /><br />                } </span>
				<span style="color: rgb(0, 0, 255);">catch</span>
				<span style="color: rgb(0, 0, 0);"> (e) {}<br /><br />            }<br /><br />        }<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (</span>
				<span style="color: rgb(0, 0, 0);">!</span>
				<span style="color: rgb(0, 0, 0);">http_request) {<br /><br />            alert('Giving up :( Cannot create an XMLHTTP instance');<br /><br />            </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">false</span>
				<span style="color: rgb(0, 0, 0);">;<br /><br />        }<br /><br />        http_request.onreadystatechange </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> alertContents;<br /><br />        http_request.open('GET', url, </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">);<br /><br />        http_request.send(</span>
				<span style="color: rgb(0, 0, 255);">null</span>
				<span style="color: rgb(0, 0, 0);">);<br /><br />    }<br /><br />    </span>
				<span style="color: rgb(0, 0, 255);">function</span>
				<span style="color: rgb(0, 0, 0);"> alertContents() {<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (http_request.readyState </span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">4</span>
				<span style="color: rgb(0, 0, 0);">) {<br /><br />            </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (http_request.status </span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">200</span>
				<span style="color: rgb(0, 0, 0);">) {<br /><br />                alert(http_request.responseText);<br /><br />            } </span>
				<span style="color: rgb(0, 0, 255);">else</span>
				<span style="color: rgb(0, 0, 0);"> {<br /><br />                alert('There was a problem </span>
				<span style="color: rgb(0, 0, 255);">with</span>
				<span style="color: rgb(0, 0, 0);"> the request.');<br /><br />            }<br /><br />        }<br /><br />    }<br /><br /></span>
				<span style="color: rgb(0, 0, 0);">&lt;/</span>
				<span style="color: rgb(0, 0, 0);">script</span>
				<span style="color: rgb(0, 0, 0);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">&lt;</span>
				<span style="color: rgb(0, 0, 0);">span<br /><br />    style</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">cursor: pointer; text-decoration: underline</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
						<br />    onclick</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">makeRequest('test.html')</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
						<br />        Make a request<br /><br /></span>
				<span style="color: rgb(0, 0, 0);">&lt;/</span>
				<span style="color: rgb(0, 0, 0);">span</span>
				<span style="color: rgb(0, 0, 0);">&gt;</span>
		</div>
		<o:p>
		</o:p>
		<p>
				<br />
		</p>
		<p>在此范例中： <o:p></o:p></p>
		<ul type="disc">
				<li class="MsoNormal">首先使用者按下「Make a request」 <o:p></o:p></li>
				<li class="MsoNormal">这么一来就会呼叫<br />     <code>makeRequest()</code> 函式，亦传入参数值 <code>test.html</code> (也就是那份 HTML 档的名称，放在同目录下) <o:p></o:p></li>
				<li class="MsoNormal">接着发出 request，而后会将主导权交给<br />     <code>onreadystatechange</code> 指定的 <code>alertContents()</code> 函式 <o:p></o:p></li>
				<li class="MsoNormal">
						<code>alertContents()</code> 检查响应是否正常，而后以 <code>alert()</code> 将 <code>test.html</code> 的内容列出 <o:p></o:p></li>
		</ul>
		<p>你可以<a href="http://www.w3clubs.com/mozdev/httprequest_test.html" target="_blank">由此测试本例</a> (<i><a target="_blank" href="http://www.w3clubs.com/mozdev/httprequest_test.html">http://www.w3clubs.com/mozdev/httprequest_test.html</a></i>)，也可以参考<a href="http://www.w3clubs.com/mozdev/test.html" target="_blank">测试档案</a> (<i><a target="_blank" href="http://www.w3clubs.com/mozdev/test.html">http://www.w3clubs.com/mozdev/test.html</a></i>)。<br /><o:p></o:p></p>
		<h2>第四步 – 「X 档案」（又称为「处理 XML 响应值」） <o:p></o:p></h2>
		<p>前面的例子中，在收到 HTTP 传回值后我们以对象的 <code>reponseText</code> 属性使用 <code>test.html</code> 档案的内容，接着来试试 <code>responseXML</code> 属性的方法。 <o:p></o:p></p>
		<p>首先，我们得做个格式正确的 XML 文件，以便稍后取用。此档名唤 <code>test.xml</code>，内容如下：</p>
		<p>
				<br />
				<o:p>
				</o:p>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 255);">&lt;?</span>
				<span style="color: rgb(255, 0, 255);">xml version="1.0" </span>
				<span style="color: rgb(0, 0, 255);">?&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
						<br />
				</span>
				<span style="color: rgb(0, 0, 255);">&lt;</span>
				<span style="color: rgb(128, 0, 0);">root</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
						<br />    I'm a test.<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">&lt;/</span>
				<span style="color: rgb(128, 0, 0);">root</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
		</div>
		<o:p>
		</o:p>
		<p>
				<br />
		</p>
		<p>在程序中，我们叫用档案的地方只须略事修改如下： <o:p></o:p></p>
		<pre>...<o:p></o:p></pre>
		<pre>
				<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 0);">onclick</span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">makeRequest('test.xml')</span>
						<span style="color: rgb(0, 0, 0);">"</span>
				</div>
				<o:p>
				</o:p>
		</pre>
		<pre>...<o:p></o:p></pre>
		<p>接着在 <code>alertContents()</code> 中，我们必须将 <code>alert(http_request.responseText);</code> 改成这样：</p>
		<p>
				<br />
				<o:p>
				</o:p>
		</p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 255);">var</span>
				<span style="color: rgb(0, 0, 0);"> xmldoc </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> http_request.responseXML;<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">var</span>
				<span style="color: rgb(0, 0, 0);"> root_node </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> xmldoc.getElementsByTagName('root').item(</span>
				<span style="color: rgb(0, 0, 0);">0</span>
				<span style="color: rgb(0, 0, 0);">);<br /><br />alert(root_node.firstChild.data);</span>
		</div>
		<br />
		<o:p>
		</o:p>
		<br />这样一来我们便可取得 <code>responseXML</code> 所传回的 <code>XMLDocument</code> 对象，而后以 DOM 相关的方法取用<br />XML 文件内容。你可以参考 <a href="http://www.w3clubs.com/mozdev/test.xml" target="_blank"><code>test.xml</code> 的原始码</a> (<i><a target="_blank" href="http://www.w3clubs.com/mozdev/test.xml">http://www.w3clubs.com/mozdev/test.xml</a></i>)<br />以及修改过后的<a href="http://www.w3clubs.com/mozdev/httprequest_test_xml.html" target="_blank">测试程序</a> (<i><a target="_blank" href="http://www.w3clubs.com/mozdev/httprequest_test_xml.html">http://www.w3clubs.com/mozdev/httprequest_test_xml.html</a></i>)。<img src ="http://www.blogjava.net/paulwong/aggbug/44684.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2006-05-05 22:21 <a href="http://www.blogjava.net/paulwong/articles/44684.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>