﻿<?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/11298.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 14 Jan 2022 23:55:31 GMT</lastBuildDate><pubDate>Fri, 14 Jan 2022 23:55:31 GMT</pubDate><ttl>60</ttl><item><title>在AJAX中将FORM里面的元素以JSON方式提交</title><link>http://www.blogjava.net/paulwong/archive/2014/08/22/417222.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Fri, 22 Aug 2014 01:38:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2014/08/22/417222.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/417222.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2014/08/22/417222.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/417222.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/417222.html</trackback:ping><description><![CDATA[<div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->$('#formID').on('submit',<span style="color: #0000FF; ">function</span>&nbsp;()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;$.ajax({<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url:&nbsp;'submit.php',<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cache:&nbsp;<span style="color: #0000FF; ">false</span>,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type:&nbsp;'POST',<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data&nbsp;:&nbsp;$('#formID').serialize(),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success:&nbsp;<span style="color: #0000FF; ">function</span>(json)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert('all&nbsp;done');<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;});<br />});</div><img src ="http://www.blogjava.net/paulwong/aggbug/417222.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> 2014-08-22 09:38 <a href="http://www.blogjava.net/paulwong/archive/2014/08/22/417222.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>AJAX 網頁程式設計─rico</title><link>http://www.blogjava.net/paulwong/archive/2006/07/15/58313.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Sat, 15 Jul 2006 05:45:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2006/07/15/58313.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/58313.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2006/07/15/58313.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/58313.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/58313.html</trackback:ping><description><![CDATA[
		<p>rico 是另外一套開放軟體的 JavaScript Framework, 根基於 prototype.js, 但是不僅僅是 prototype.js 的延伸而已, 還創造出一些它獨有的功能, 其中包含以下幾個特色：</p>
		<p>
				<br />
		</p>1. 支援 AJAX。<br />2. 支援拖拉式介面。<br />3. 動畫效果, 如動態改變元件位置大小等。<br /><br /><div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: rgb(128,0,0)">rico 下載網址：<a href="http://openrico.org/rico/home.page">http://openrico.org/rico/home.page</a></span></div><br /><br />rico 的套用方式也非常簡單, 不過它需要配合 prototype.js, 因此必須在 HTML 文件中同時套用這兩套 Framework, 方法如下。<br /><br /><br /><div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)"><!--<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(128,0,0)">head</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">script </span><span style="COLOR: rgb(255,0,0)">type</span><span style="COLOR: rgb(0,0,255)">="text/javascript"</span><span style="COLOR: rgb(255,0,0)"> src</span><span style="COLOR: rgb(0,0,255)">="script/prototype.js"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">script</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">script </span><span style="COLOR: rgb(255,0,0)">type</span><span style="COLOR: rgb(0,0,255)">="text/javascript"</span><span style="COLOR: rgb(255,0,0)"> src</span><span style="COLOR: rgb(0,0,255)">="script/rico.js"</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;/</span><span style="COLOR: rgb(128,0,0)">script</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">head</span><span style="COLOR: rgb(0,0,255)">&gt;</span></div><br /><br /><p><font color="#ff6600">rico 範例：旅遊網站</font></p><p>rico 的 ajax 實作是以一個 ajaxEngine 物件為核心, 任何 ajax 的動作都是透過這個物件來完成, 事實上, 它的觀念也非常簡單, 非常適合初次使用 ajax 的人學習。在這個小節中, 筆者將透過簡單的範例來介紹 rico ajax 的使用。</p><p><br /></p><p>我們設計了一個「台灣走透透 — 縣市情報通」的網站, 主要功能是提供使用者查詢台灣各縣市資訊及小吃特產等資訊, 類似旅遊資訊之類的情報站。但是如果我們只是單純將各個縣市的情報寫成 HTML 檔案, 然後讓使用者點選超連結瀏覽, 這樣就不符合前面章節所提的非同步通訊的好處, 所以在這裡我們就改變設計, 讓使用者選擇想要瀏覽的縣市之後, 立刻出現該縣市相關資訊, 當然在這裡我們是要學習 AJAX, 所以這個範例就是利用 AJAX 來完成這樣的動作。</p><p><br /></p><p>整個 HTML 檔案主要是由一個 select box 和一個 &lt;DIV&gt; 標籤所構成, 當使用者選取了 select box 中任一個地名時, 右手邊的 &lt;DIV&gt; 區塊便會被置換成相對應的地區介紹, 當然這些介紹文字都是經由 AJAX 向伺服器要來的。</p><br /><p></p><div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: rgb(0,0,0)">rico_exam.html<br />01 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">html</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />02 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">head</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />03 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">meta </span><span style="COLOR: rgb(255,0,0)">http-equiv</span><span style="COLOR: rgb(0,0,255)">="Content-Type"</span><span style="COLOR: rgb(255,0,0)"><br />04 content</span><span style="COLOR: rgb(0,0,255)">="text/html; charset=UTF-8"</span><span style="COLOR: rgb(255,0,0)"> </span><span style="COLOR: rgb(0,0,255)">/&gt;</span><span style="COLOR: rgb(0,0,0)"><br />05 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">script </span><span style="COLOR: rgb(255,0,0)">type</span><span style="COLOR: rgb(0,0,255)">="text/javascript"</span><span style="COLOR: rgb(255,0,0)"> src</span><span style="COLOR: rgb(0,0,255)">="script/prototype.js"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"><br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">06</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> </span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">script</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />07 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">script </span><span style="COLOR: rgb(255,0,0)">type</span><span style="COLOR: rgb(0,0,255)">="text/javascript"</span><span style="COLOR: rgb(255,0,0)"> src</span><span style="COLOR: rgb(0,0,255)">="script/rico.js"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"><br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">08</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> </span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">script</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />09<br />10 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">script </span><span style="COLOR: rgb(255,0,0)">type</span><span style="COLOR: rgb(0,0,255)">="text/javascript"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"><br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">11</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> </span><span style="COLOR: rgb(0,0,255); BACKGROUND-COLOR: rgb(245,245,245)">var</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> cur_sel;<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">12</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> </span><span style="COLOR: rgb(0,0,255); BACKGROUND-COLOR: rgb(245,245,245)">function</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> init() {<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">13</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> cur_sel </span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">=</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> '</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">0</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">';<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">14</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> document.getElementById('selField').value</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">=</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">cur_sel;<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">15</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> </span><span style="COLOR: rgb(0,128,0); BACKGROUND-COLOR: rgb(245,245,245)">//</span><span style="COLOR: rgb(0,128,0); BACKGROUND-COLOR: rgb(245,245,245)"> 向 ajaxEngine 註冊一個新的要求 - AID_LOCQUERY</span><span style="COLOR: rgb(0,128,0); BACKGROUND-COLOR: rgb(245,245,245)"><br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">16</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> 【ajaxEngine.registerRequest('AID_LOCQUERY', 'loc.php');】<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">17</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> </span><span style="COLOR: rgb(0,128,0); BACKGROUND-COLOR: rgb(245,245,245)">//</span><span style="COLOR: rgb(0,128,0); BACKGROUND-COLOR: rgb(245,245,245)"> 向 ajaxEngine 註冊一個新的元件 - descField</span><span style="COLOR: rgb(0,128,0); BACKGROUND-COLOR: rgb(245,245,245)"><br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">18</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> 【ajaxEngine.registerAjaxElement('descField');】<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">19</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> }<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">20</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"><br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">21</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> </span><span style="COLOR: rgb(0,0,255); BACKGROUND-COLOR: rgb(245,245,245)">function</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> queryLocDesc(sel) {<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">22</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> </span><span style="COLOR: rgb(0,0,255); BACKGROUND-COLOR: rgb(245,245,245)">var</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> locName </span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">=</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> sel.value;<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">23</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> </span><span style="COLOR: rgb(0,0,255); BACKGROUND-COLOR: rgb(245,245,245)">if</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> (locName </span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">!=</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> '</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">0</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">' </span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">&amp;&amp;</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> locName </span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">!=</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> cur_sel) {<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">24</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> </span><span style="COLOR: rgb(0,128,0); BACKGROUND-COLOR: rgb(245,245,245)">//</span><span style="COLOR: rgb(0,128,0); BACKGROUND-COLOR: rgb(245,245,245)"> 送出 AID_LOCQUERY 的要求，並指定參數</span><span style="COLOR: rgb(0,128,0); BACKGROUND-COLOR: rgb(245,245,245)"><br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">25</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> 【ajaxEngine.sendRequest ('AID_LOCQUERY', 】<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">26</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> 【'locName</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">=</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">'</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">+</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">locName);】<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">27</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> cur_sel </span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">=</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> locName;<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">28</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> }<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">29</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> }<br /></span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)">30</span><span style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"> </span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">script</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />31 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">title</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)">台灣走透透 — 縣市情報通</span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">title</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />32 </span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">head</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />33 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">body </span><span style="COLOR: rgb(255,0,0)">onload</span><span style="COLOR: rgb(0,0,255)">="init()"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />34<br />35 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">table </span><span style="COLOR: rgb(255,0,0)">width</span><span style="COLOR: rgb(0,0,255)">="50%"</span><span style="COLOR: rgb(255,0,0)"> border</span><span style="COLOR: rgb(0,0,255)">=0</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />36 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">tr</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />37 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">td </span><span style="COLOR: rgb(255,0,0)">width</span><span style="COLOR: rgb(0,0,255)">="25%"</span><span style="COLOR: rgb(255,0,0)"> valign</span><span style="COLOR: rgb(0,0,255)">="top"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />38 請選擇縣市情報：<br />39 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">select </span><span style="COLOR: rgb(255,0,0)">id</span><span style="COLOR: rgb(0,0,255)">="selField"</span><span style="COLOR: rgb(255,0,0)"> onchange</span><span style="COLOR: rgb(0,0,255)">="queryLocDesc(this)"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />40 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">option </span><span style="COLOR: rgb(255,0,0)">value</span><span style="COLOR: rgb(0,0,255)">="0"</span><span style="COLOR: rgb(255,0,0)"> selected</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)">----------</span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">option</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />41 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">option </span><span style="COLOR: rgb(255,0,0)">value</span><span style="COLOR: rgb(0,0,255)">="1"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)">台北市</span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">option</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />42 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">option </span><span style="COLOR: rgb(255,0,0)">value</span><span style="COLOR: rgb(0,0,255)">="2"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)">台北縣</span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">option</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />43 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">option </span><span style="COLOR: rgb(255,0,0)">value</span><span style="COLOR: rgb(0,0,255)">="3"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)">基隆市</span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">option</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />44 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">option </span><span style="COLOR: rgb(255,0,0)">value</span><span style="COLOR: rgb(0,0,255)">="4"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)">宜蘭縣</span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">option</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />45 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">option </span><span style="COLOR: rgb(255,0,0)">value</span><span style="COLOR: rgb(0,0,255)">="5"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)">桃園縣</span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">option</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />46 </span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">select</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />47 </span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">td</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />48 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">td</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />49 </span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">div </span><span style="COLOR: rgb(255,0,0)">id</span><span style="COLOR: rgb(0,0,255)">="descField"</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;/</span><span style="COLOR: rgb(128,0,0)">div</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />50 </span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">td</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />51<br />52 </span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">tr</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />53 </span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">table</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />54<br />55 </span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">body</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br />56 </span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">html</span><span style="COLOR: rgb(0,0,255)">&gt;</span></div><br /><p></p><br /><p><font color="#ff6600">執行結果</font></p><br /><p><img height="228" src="http://www.flag.com.tw/db/preview/images/F6475_1_a.jpg" width="337" /></p><br /><p><br /></p><p><font color="#ff6600">程式說明</font></p><p><img height="436" hspace="10" src="http://www.flag.com.tw/db/preview/images/F6475_1_b.jpg" width="96" align="right" vspace="10" />這個範例的流程圖如右:</p><p> </p><p>這個範例看起來簡單多了, 這也是我們為什麼要使用 Framework 的目的之一, 重複利用別人的成果, 可以為自己解省力氣, 並專注在開發更精緻的產品上。</p><p><br /></p><p>在範例一開始, 我們首先要載入兩個 JavaScript 程式庫, 一個是前面所介紹的 prototype.js, 而一個是 rico.js, 因為 rico.js 實際上是植基於 prototype.js 上, 所以在使用 rico 之前, 務必要先載入 prototype.js, 而且要使用對的版本, 例如在筆者撰寫本章節時, rico 最新的版本是 1.1.0, 它所需要的 prototype.js 最低需求是 1.4.0, 所以你如果要使用這個版本, 同樣的也需要一份 prototype.js 1.4.0 的函式庫。</p><p><br /></p><p>在載入 prototype.js 以及 rico.js 之後, 接下來的 script 區塊便是我們自己的程式碼了, 在這個範例中, 筆者只用了短短兩個函式, 第一個函式 init() 是用來在頁面被載入的同時做初始化, 另外一個函式 queryLocDesc() , 是當使用者選擇了 select box 中不同的選項時, 做出對應的動作, 所以我們必須在 select box 的 onchange 事件被觸發時去執行這個函式。</p><p><br /></p><p>前面有提到 rico 的 AJAX 是以 ajaxEngine 為核心, 所以整個頁面不管有幾個 AJAX 需求, 都是要跟這個物件註冊, 在第一個範例中, 我們只有一種 AJAX 需求, 所以我們也只對 ajaxEngine 註冊一個要求, 在 rico 中, 你要完成這個動作就是要呼叫 registerRequest() 函式, 不過註冊的動作並不會觸發 XMLHttpRequest 立刻去執行, 而是必須等到我們呼叫了 sendRequest() 這個函式後, ajaxEngine 才會真正有動作。</p><p><br /></p><p>因為我們可以對 ajaxEngine 註冊好幾個 AJAX 需求, 為了區別不同的需求, 便需要一個 ID 來辨識, rico 是以一個字串 ID 來為每個 AJAX 需求作辨認, 所以不管你呼叫 registerRequest() 以及 sendRequest() 時都要指定這個 ID, 讓 rico 可以知道你要做的是那個 AJAX 動作, 在我們第一個範例中, 就是以 AID_LOCQUERY 作為這個 ID 字串。又例如以下程式碼便是我們註冊了好幾個 AJAX 需求, 且個別去執行動作。</p><br /><p></p><div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: rgb(0,0,255)">function</span><span style="COLOR: rgb(0,0,0)"> init() {<br />ajaxEngine.registerRequest('AID_FOODQUERY', 'food.php');<br />ajaxEngine.registerRequest('AID_FUNQUERY', 'fun.php');<br />ajaxEngine.registerRequest('AID_INFO', 'info.php');<br />}<br /><br /></span><span style="COLOR: rgb(0,0,255)">function</span><span style="COLOR: rgb(0,0,0)"> queryFood() {<br />ajaxEngine.sendRequest('AID_FOODQUERY');<br />}<br /><br /></span><span style="COLOR: rgb(0,0,255)">function</span><span style="COLOR: rgb(0,0,0)"> queryFun() {<br />ajaxEngine.sendRequest('AID_FUNQUERY');<br />}<br /><br /></span><span style="COLOR: rgb(0,0,255)">function</span><span style="COLOR: rgb(0,0,0)"> queryInfo() {<br />ajaxEngine.sendRequest('AID_INFO');<br />}</span></div><br /><p>利用 rico 開發 AJAX 有個限制, 也就是你必須給它適當格式的 XML 文件, 底下便是我們給第一個範例的 XML 文件：</p><br /><p></p><div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)"><!--<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" encoding="UTF-8"</span><span style="COLOR: rgb(0,0,255)">?&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">ajax-response</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">response </span><span style="COLOR: rgb(255,0,0)">type</span><span style="COLOR: rgb(0,0,255)">="element"</span><span style="COLOR: rgb(255,0,0)"> id</span><span style="COLOR: rgb(0,0,255)">="descField"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">img </span><span style="COLOR: rgb(255,0,0)">src</span><span style="COLOR: rgb(0,0,255)">="/images/sights/taipeicity00.jpg"</span><span style="COLOR: rgb(255,0,0)"> </span><span style="COLOR: rgb(0,0,255)">/&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">table </span><span style="COLOR: rgb(255,0,0)">border</span><span style="COLOR: rgb(0,0,255)">="0"</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">tr</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;</span><span style="COLOR: rgb(128,0,0)">td</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;</span><span style="COLOR: rgb(128,0,0)">b</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)">面積</span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">b</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)">： 271.7997平方公里</span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">td</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;/</span><span style="COLOR: rgb(128,0,0)">tr</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">tr</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;</span><span style="COLOR: rgb(128,0,0)">td</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;</span><span style="COLOR: rgb(128,0,0)">b</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)">人口</span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">b</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)">：264萬6474人(2001.6)</span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">td</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;/</span><span style="COLOR: rgb(128,0,0)">tr</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">tr</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;</span><span style="COLOR: rgb(128,0,0)">td</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;</span><span style="COLOR: rgb(128,0,0)">b</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)">知 名 小 吃：</span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">b</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;/</span><span style="COLOR: rgb(128,0,0)">td</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;/</span><span style="COLOR: rgb(128,0,0)">tr</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(128,0,0)">tr</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;</span><span style="COLOR: rgb(128,0,0)">td</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)">td</span><span style="COLOR: rgb(0,0,255)">&gt;&lt;/</span><span style="COLOR: rgb(128,0,0)">tr</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">table</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">response</span><span style="COLOR: rgb(0,0,255)">&gt;</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">&lt;/</span><span style="COLOR: rgb(128,0,0)">ajax-response</span><span style="COLOR: rgb(0,0,255)">&gt;</span></div><br /><p>在每個給 rico ajaxEngine 的 XML 文件中, 必須是由 &lt;ajax-response&gt; &lt;/ajax-response&gt; 所包裝起來, 而在這個區塊中, 你又必須將你真正的資料包裝在 &lt;response&gt; &lt;/response&gt; 區塊中, 不過在同一個 &lt;ajax-response&gt; 區塊中, 可以包含多個 &lt;response&gt; 區塊。</p><p><br /></p><p>在每個 &lt;response&gt; 的標籤中, 你必須描述這個回應的屬性和辨識 ID, 屬性可以是 element 或者是 object, 端看你向 ajaxEngine 註冊的是 element 或者是 object 而定, 例如在第一個範例中, 我們註冊了一個 element, 它的 ID 是 descField：</p><br /><p></p><div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: rgb(0,0,0)">ajaxEngine.registerAjaxElement('descField');</span></div><br /><p>不過, 在你的 HTML 文件中, 你所註冊的 ID 必須要有相對應的 &lt;DIV&gt; 區塊, 因為當 ajaxEngine 收到伺服器的回應後, 會對這個 &lt;DIV&gt; 區塊做替換的工作, 例如, 在第一個範例中, ajaxEngine 會將 &lt;response&gt; &lt;/response&gt; 中所夾帶的 HTML 內容替換到 &lt;DIV id="descField"&gt; &lt;/DIV&gt; 中, 因此在瀏覽器上, 我們就可以看到資料的呈現。</p><img src ="http://www.blogjava.net/paulwong/aggbug/58313.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-07-15 13:45 <a href="http://www.blogjava.net/paulwong/archive/2006/07/15/58313.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>AJAX 上手篇</title><link>http://www.blogjava.net/paulwong/archive/2006/05/18/46904.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Thu, 18 May 2006 13:56:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2006/05/18/46904.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/46904.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2006/05/18/46904.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/46904.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/46904.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="../../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><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<br />
						<span style="color: rgb(0, 0, 0);">http_request.setRequestHeader('Content</span>
						<br />
						<span style="color: rgb(0, 0, 0);">-</span>
						<br />
						<span style="color: rgb(0, 0, 0);">Type', 'application</span>
						<br />
						<span style="color: rgb(0, 0, 0);">/</span>
						<br />
						<span style="color: rgb(0, 0, 0);">x</span>
						<br />
						<span style="color: rgb(0, 0, 0);">-</span>
						<br />
						<span style="color: rgb(0, 0, 0);">www</span>
						<br />
						<span style="color: rgb(0, 0, 0);">-</span>
						<br />
						<span style="color: rgb(0, 0, 0);">form</span>
						<br />
						<span style="color: rgb(0, 0, 0);">-</span>
						<br />
						<span style="color: rgb(0, 0, 0);">urlencoded');<br /><br /></span>
						<br />
				</div>
				<br />
				<br />
				<br />
				<o:p>
						<br />
				</o:p>
				<br />
		</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="../../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><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<br />
						<span style="color: rgb(0, 0, 0);">onclick</span>
						<br />
						<span style="color: rgb(0, 0, 0);">=</span>
						<br />
						<span style="color: rgb(0, 0, 0);">"</span>
						<br />
						<span style="color: rgb(0, 0, 0);">makeRequest('test.xml')</span>
						<br />
						<span style="color: rgb(0, 0, 0);">"</span>
						<br />
				</div>
				<br />
				<o:p>
						<br />
				</o:p>
				<br />
		</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/46904.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-18 21:56 <a href="http://www.blogjava.net/paulwong/archive/2006/05/18/46904.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>