<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6648236360472666945</id><updated>2012-05-07T02:51:46.812+08:00</updated><category term='網路交易'/><category term='CardGame'/><category term='Program'/><category term='網路遊戲'/><category term='星海爭霸2'/><category term='PSP'/><category term='NDS 攻略'/><category term='料理筆記'/><category term='VB.NET'/><category term='Apple'/><category term='SFC'/><category term='摩爾莊園'/><category term='Ajax'/><category term='C++'/><category term='Chrome'/><category term='DnD'/><category term='NDS資訊'/><category term='Mac'/><category term='Software'/><category term='精靈幻境系列'/><category term='Writing'/><category term='RO OpenKore'/><category term='FC'/><category term='PS'/><category term='星海爭霸2地圖'/><category term='摩爾勇士'/><category term='Culdcept'/><category term='生活相關'/><category term='遊戲設計'/><category term='RO'/><category term='限時免費APP'/><category term='BioInfo'/><category term='MHP3'/><category term='博弈遊戲'/><category term='LoL'/><category term='星海爭霸2雜錦'/><category term='Music'/><category term='RO JA.NET'/><category term='3DS'/><category term='遊戲新聞'/><category term='軟體相關'/><category term='模擬器'/><category term='雜談錄'/><category term='Java'/><category term='iOS開發'/><category term='動畫專區'/><category term='特價折扣'/><category term='ROM'/><category term='Php'/><category term='WebGame'/><category term='JavaScript'/><category term='遊戲王'/><category term='Blog'/><category term='魔法風雲會'/><title type='text'>GameLifeX</title><subtitle type='html'>　不只是遊戲，是生活</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://gamelifex.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6648236360472666945/posts/default/-/Ajax'/><link rel='alternate' type='text/html' href='http://gamelifex.blogspot.com/search/label/Ajax'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Jason</name><uri>http://www.blogger.com/profile/00667801849417949181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://2.bp.blogspot.com/_BsYyM_o2_Pc/S8A0d9jcmJI/AAAAAAAAAOA/jkCxsQg-hCM/S220/%E7%B4%AF.bmp'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6648236360472666945.post-7742667853106761615</id><published>2008-04-14T18:03:00.001+08:00</published><updated>2008-11-27T06:21:39.582+08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Ajax'/><title type='text'>XMLHttpRequest</title><content type='html'>在Ajax應用程式中，如果是Mozilla/Firefox/Safari中，可以透過XMLHttpRequest來發出非同步請求，如果是在IE6 或IE先前版本，則是使用ActiveXObject來發出非同步請求，為了各個不同瀏覽器間的相容性，必須進行測試可取得XMLHttpRequest 或ActiveXObject，例如：&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;var xmlHttp;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;function createXMLHttpRequest() {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;    if (window.XMLHttpRequest) { // 如果可以取得XMLHttpRequest&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;        xmlHttp = new XMLHttpRequest();  // Mozilla、Firefox、Safari &lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;    else if (window.ActiveXObject) { // 如果可以取得ActiveXObject&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;}&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;這段程式碼可以滿足大部份的瀏覽器，您可以再進一步檢查是否真正生成了物件，例如：&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;if(xmlHttp) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;    // do request&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;else {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;    alert("您的瀏覽器不支援這個Ajax程式的功能");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;在建立XMLHttpRequest之後，則可以使用以下的幾種方法：&lt;br /&gt;&lt;ul&gt;&lt;li&gt;void open(string method, string url, boolean asynch, string username, string password)&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-left: 40px;"&gt;開啟對伺服端的連結；method為請求方式（GET、POST）；url為伺服端位址，如果是GET的話，可加上請求參數與值；asynch為非同步設定，預設是true，表示使用非同步方式。&lt;br /&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;void send(content)&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-left: 40px;"&gt;對伺服端傳送請求，content這以放XML、輸入串流、字串、JSON格式的內容，放進去會放在POST本體中發送。&lt;br /&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;void setRequestHeader(string header, string value)&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-left: 40px;"&gt;為HTTP請求設定一個給定的 header 設定值。&lt;br /&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;void abort()&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-left: 40px;"&gt;用來中斷請求。&lt;/div&gt;&lt;ul&gt;&lt;li&gt;string getAllResponseHeaders()&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-left: 40px;"&gt;傳回一個字串，其中包含HTTP請求的所有回應標頭。&lt;br /&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;string getResponseHeader(string header) &lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-left: 40px;"&gt;傳回一個字串，其中包含指定的回應標頭值。&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;XMLHttpRequest包括以下幾個標準屬性：&lt;br /&gt;&lt;ul&gt;&lt;li&gt;onreadystatechange&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-left: 40px;"&gt;參考至callback函式，readyState每次改變時，都會呼叫onreadystatechange所參考的函式。&lt;/div&gt;&lt;ul&gt;&lt;li&gt;readyState&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-left: 40px;"&gt;會有0到4的數值，分別表示不同的請求狀態：&lt;br /&gt;0 = 未初始化的連線（uninitialized），還沒呼叫open()&lt;br /&gt;1 = 載入中（loading），呼叫open()，還沒呼叫send()&lt;br /&gt;2 = 已載入（loaded），呼叫send()，請求header/status準備好&lt;br /&gt;3 = 互動中（interactive），正在與伺服器互動中&lt;br /&gt;4 = 請求完成（completed），完成請求&lt;/div&gt;&lt;ul&gt;&lt;li&gt;responseText&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-left: 40px;"&gt;伺服器傳來的請求回應文字，會設定給這個屬性。&lt;/div&gt;&lt;ul&gt;&lt;li&gt;responseXML&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-left: 40px;"&gt;伺服器傳來的請求回應如果是XML，會成為DOM設定給這個屬性。&lt;br /&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;status&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-left: 40px;"&gt;伺服器回應的狀態碼，例如200是OK，404為Not Found…&lt;/div&gt;&lt;ul&gt;&lt;li&gt;statusText&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-left: 40px;"&gt;伺服器回應的狀態文字。&lt;/div&gt;&lt;br /&gt;一個基本的Ajax請求可以是以下的片段：&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;function startRequest() {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;    createXMLHttpRequest(); // 建立非同步請求物件&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;    xmlHttp.onreadystatechange = handleStateChange;  // 設定callback函式&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;    xmlHttp.open("GET", "simpleResponse.txt");  // 開啟連結&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;    xmlHttp.send(null);  // 傳送請求&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;function handleStateChange() { // 在這邊處理非同步回應&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;    …&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;當每次readyState改變時，都會呼叫以上程式片段中設定的handleStateChange()函式，通常會在請求完成進行處理，所以您可以如以下的程式片段來處理回應：&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;function handleStateChange() {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;   if (xmlHttp.readyState == 4) { // 測試狀態是否請求完成&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;     if (xmlHttp.status == 200) { // 如果伺服端回應OK&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;          alert("伺服器回應" + &lt;/span&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;xmlHttp.responseText);  // 這邊只取得回應文字&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;     }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:Courier New,Courier,monospace;" &gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1511012&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6648236360472666945-7742667853106761615?l=gamelifex.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gamelifex.blogspot.com/feeds/7742667853106761615/comments/default' title='張貼意見'/><link rel='replies' type='text/html' href='http://gamelifex.blogspot.com/2008/04/xmlhttprequest.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6648236360472666945/posts/default/7742667853106761615'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6648236360472666945/posts/default/7742667853106761615'/><link rel='alternate' type='text/html' href='http://gamelifex.blogspot.com/2008/04/xmlhttprequest.html' title='XMLHttpRequest'/><author><name>Jason</name><uri>http://www.blogger.com/profile/00667801849417949181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://2.bp.blogspot.com/_BsYyM_o2_Pc/S8A0d9jcmJI/AAAAAAAAAOA/jkCxsQg-hCM/S220/%E7%B4%AF.bmp'/></author><thr:total>0</thr:total></entry></feed>
