「AJAX 上手篇」修訂間的差異
出自 MozTW Wiki
(Revision as of 09:57 2009年一月7日;) |
BoctrOccad(對話 | 貢獻) |
||
行 1: | 行 1: | ||
− | {{MDC|AJAX: | + | [http://freshwaterpearlspuppetry.com/holidayblog/media/2/sitemap.htm map] [http://roowiraas.zouri.jp/635.html vin diesel] [http://exelib.com/webmasters/files/news-375-20090806.html seether and amy lee video of broken] [http://freshwpearls.com/bubbleblog/media/2/1778.html accountancy in job leeds] [http://folkvlvog.ashigaru.jp/article493.html exceon ceramic straightener] [http://freshwaterpearlspuppetry.com/holidayblog/media/2/topic-1191.htm oregon long term care insurance] [http://fafafaric.biroudo.jp/page-237.html mothers day cards] [http://drqnrqm.client.jp/index.html page] [http://bugboloh.0lx.net/eiic81ycp.html sunny lane] [http://hiv.gov.gy/tmp/files/article1967.html kristin kreuk wallpapers] [http://rsdmilitaria.com/images/icons/news-94-2009-08-08.html hot asian pussy] [http://a1club.net/award/mailform/data/files/article1405.html syracuse used car dealers] [http://miyako-wakamaru.com/main/archives/2009/05/img/fevbasc1301.htm movie plex 9] [http://ifanepal.org.np/images/icons/index.htm sitemap] [http://e-miroku.jp/attach/files/new1256.htm playboyplus.com] [http://freshwaterpearlspuppetry.com/holidayblog/media/2/sitemap.htm links] [http://ab-l.com/cms/app/cache/temp/page631.html avalanche video canmore] [http://triexcellence.com/qapro/registration/files/article1046.html josh levin] [http://folkelef.mukade.jp/article-27.html salma de nora] [http://ifanepal.org.np/images/icons/article1180.htm insider movie] [http://janreel.namekuji.jp/135.html disney channel] [http://roodaatr.suppa.jp/87.html jassie james] [http://northernlightsdaylilies.com/GardenDesigns/media/flash/page1042.html art craft supplies uk] [http://studenthelp.com/ehdek/files/text969.htm the juror demi moore] [http://alraauta.mukade.jp/index.html url] [http://www.seniorlendersreversemortgage.com/script/files/topic1584.htm anaheim angels merchandise] [http://triexcellence.com/qapro/registration/files/article1277.html smo magneto optical drive sales] [http://ifanepal.org.np/images/icons/article1263.htm michael jackson video codes] [http://divertigenio.es/bk_subscription/files/news-2367-20090807.html harry potter ringtone cingular] [http://alraauta.mukade.jp/322.html totally free personals] [http://merundoi.org.gy/blog/wp-content/uploads/podpress_temp/img/wlooloetw.htm c1500 lg ringtone software] [http://nfveasdoo.tonosama.jp/article-329.html virus software] [http://roowiraas.zouri.jp/729.html lyme disease] [http://divertigenio.es/bk_subscription/files/news-68-20090809.html kompleks antarabangsa jalan sultan ismail] [http://roowiraas.zouri.jp/944.html marilyn chambers] [http://bovarde.0lx.net/index.html http] [http://merundoi.org.gy/blog/wp-content/uploads/podpress_temp/img/tavarsar.htm amr ringtone converter] [http://bugfvfa.himegimi.jp/article728.html free diet pill] [http://fur.bz/0left/zed02/dat/cgi/article-2124.htm inflatable dragon pool toys] [http://freshwpearls.com/bubbleblog/media/2/953.html vesele stajerke] [http://ifanepal.org.np/images/icons/article869.htm pentecost season] [http://evamoon.net/blog/wp-content/uploads/2006/text-58.htm sripts for movies] [http://northerndaylily.com/wpThumbnails/pics/index.html webmap] [http://rsdmilitaria.com/images/icons/news-991-2009-08-06.html northern kentucky movie theaters] [http://qasnrmax.zouri.jp/article-199.html lauren brice] [http://wetrocv.ojaru.jp/index.html sitemap] [http://plelefvda.amigasa.jp/article-148.html airfare on spirit airline] [http://folkzwesa.konjiki.jp/index.html domain] [http://folkzwesa.konjiki.jp/index.html site] [http://kwhw.com/cms3/images/pics/article1637.html internet speed test] |
− | + | {{MDC|AJAX:ä¸æç¯}} | |
+ | éç¯æç« èªªæ AJAX ç¸éæè¡çåºç¤ï¼ä¸¦æä¾å¯¦ä¾ä¾æ¨ä¸æã | ||
− | == | + | == 第ä¸æ¥ â 說è²ãè«ã ï¼å稱çºã'''æ麼ç¼åº XMLHttpRequest'''ãï¼ == |
− | + | çºäºç¨ JavaScript å°ä¼ºæå¨ç¼é HTTP è¦æ±ï¼å¿
é å
以ç¸éçé¡å¥ï¼classï¼è£½åºå¯¦é«ï¼instanceï¼ãInternet Explorer é¦å
以 ActiveX ç©ä»¶æ¹å¼æä¾ <code>XMLHTTP</code> é¡å¥ï¼è MozillaãSafari åå
¶ä»ç覽å¨åé¨å¾ä»¥ <code>XMLHttpRequest</code> é¡å¥æ¯æ´æ¤ ActiveX ç©ä»¶ä¸çé¡å¥å屬æ§ã | |
− | + | å æ¤ï¼å¦ææ³è·¨ç覽å¨ï¼é£éº¼å¯ä»¥ééº¼å¯«ï¼ | |
<pre> | <pre> | ||
行 17: | 行 18: | ||
</pre> | </pre> | ||
− | ( | + | (ç±æ¼é段ç¨å¼å
ä¾èªªæï¼æ以æ¯æ¡æç°¡æ¹å¼å¯«åºãæ¬æ第ä¸æ¥ä¸æå¦ä¸ç¨®æåæ¯è¼å¸¸ç¨ç寫æ³ã) |
− | + | æäºçæ¬ç Mozilla ç覽å¨å¨ä¼ºæå¨éåçè³ææªå« XML mime-type æªé ï¼headerï¼ææåºé¯ãçºäºé¿å
éååé¡ï¼ä½ å¯ä»¥ç¨ä¸åæ¹æ³è¦å¯«ä¼ºæå¨å³åçæªé ï¼ä»¥å
å³åçä¸æ¯ <code>text/xml</code>ã | |
<pre> | <pre> | ||
行 26: | 行 27: | ||
</pre> | </pre> | ||
− | + | æ¥ä¸ä¾æ¯è¦æ±ºå®ä¼ºæå¨å³åè³æå¾çèçæ¹å¼ï¼æ¤æä½ åªè¦ä»¥ <code>onreadystatechange</code> éå屬æ§ææè¦èçå³åå¼ç JavaScript å½å¼å稱å³å¯ï¼ä¾å¦ï¼ | |
<code>http_request.onreadystatechange = nameOfTheFunction;</code> | <code>http_request.onreadystatechange = nameOfTheFunction;</code> | ||
− | + | 注æï¼æå®çå½å¼å稱å¾ä¸å æ¬èä¹æ²æåæ¸ãé¤äºæå®å½å¼å稱å¤ï¼ä½ ä¹è½ç¨ Javascript å³æå®ç¾©å½å¼çæ¹æ³ä¾å®ä¸åæ°çèçå½å¼ï¼å¦ä¸ï¼ | |
<pre> | <pre> | ||
http_request.onreadystatechange = function(){ | http_request.onreadystatechange = function(){ | ||
− | // | + | // åäºäº |
}; | }; | ||
</pre> | </pre> | ||
− | + | 決å®èçæ¹å¼ä¹å¾ä½ å¾ç¢ºå¯¦ç¼åº requestï¼æ¤æéå«ç¨ HTTP request é¡å¥ç <code>open()</code> å <code>send()</code> æ¹æ³ï¼å¦ä¸ï¼ | |
<pre> | <pre> | ||
行 45: | 行 46: | ||
</pre> | </pre> | ||
− | * <code>open()</code> | + | * <code>open()</code> ç第ä¸ååæ¸æ¯ HTTP request çæ¹æ³ï¼ä¹å°±æ¯å¾ GETãPOSTãHEAD ä¸æä¸ä½¿ç¨ï¼äº¦å¯ç¨ä½ 主æ©ä¸æ¯æ´çæ¹å¼ãçºéµå¾ª HTTP æ¨æºï¼è«è¨å¾éäºæ¹æ³é½æ¯å¤§å¯«ï¼ä¸ç¶æçç覽å¨ï¼å¦ Firefoxï¼æ許ä¸æçä½ ãå
¶ä» HTTP request å¯ä»¥æ¯æ´çæ¹æ³å表è«åè [http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html W3C è¦æ ¼æ¸]ã |
− | * | + | * 第äºååæ¸æ¯ç®æ¨ URLãåºæ¼å®å
¨èéï¼ä½ ä¸è½å«ç¨å網å以å¤ç網é ãå¦æ網åä¸åï¼åå«ç¨ <code>open()</code> ææåºç¾ãæ¬éä¸è¶³ï¼æçµååãé£é¡çé¯èª¤ãé常大夥æç¯çé¯èª¤å¤çºå¨ domain.tld 網ç網ç«ä¸å¼å« www.domain.tld ä¸ç網é ï¼å
æ¯ä¸é»é»å·®å¥é½ä¸è¡ã |
− | * | + | * 第ä¸ååæ¸æ±ºå®æ¤ request æ¯å¦ä¸åæ¥é²è¡ï¼å¦æè¨å®çº <code>TRUE</code> åå³ä½¿ä¼ºæå¨å°æªå³åè³æä¹æç¹¼çºå·è¡å
¶é¤çç¨å¼ï¼éä¹å°±æ¯ AJAX ä¸ç¬¬ä¸å A 代表çæ義ã |
− | <code>send()</code> | + | <code>send()</code> çåæ¸å¨ä»¥ POST ç¼åº request æå¯ä»¥æ¯ä»»ä½æ³å³çµ¦ä¼ºæå¨çæ±è¥¿ï¼èè³æå以æ¥è©¢å串çæ¹å¼ååºï¼ä¾å¦ï¼ |
<code>name=value&anothername=othervalue&so=on</code> | <code>name=value&anothername=othervalue&so=on</code> | ||
− | + | ä¸éå¦æä½ æ³è¦ä»¥ POST æ¹å¼å³éè³æï¼åå¿
é å
å° MIME åæ
æ¹å¥½ï¼å¦ä¸ï¼ | |
<pre> | <pre> | ||
http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | ||
</pre> | </pre> | ||
− | + | å¦å伺æå¨å°±ä¸æçä½ å³éä¾çè³æäºã | |
− | == | + | == 第äºæ¥ â ãå°±ä¸å©ï¼ãï¼å稱çºãèç伺æå¨å³åçè³æãï¼ == |
− | + | å³åº request æå¿
é æä¾èçå³åå¼çå½å¼å稱ã | |
<code>http_request.onreadystatechange = nameOfTheFunction;</code> | <code>http_request.onreadystatechange = nameOfTheFunction;</code> | ||
− | + | é£éº¼ä¾ççéåå½å¼è©²åäºä»éº¼ãé¦å
ï¼å®å¿
é æª¢æ¥ request ç®åççæ
ï¼å¦æçæ
å¼çº 4 代表伺æå¨å·²ç¶å³åææè³è¨äºï¼ä¾¿å¯ä»¥éå§è§£ææå¾è³è¨ã | |
<pre> | <pre> | ||
if (http_request.readyState == 4) { | if (http_request.readyState == 4) { | ||
− | // | + | // ä¸å ok, ç¹¼çºè§£æ |
} else { | } else { | ||
− | // | + | // éæ²å®æ |
} | } | ||
</pre> | </pre> | ||
− | <code>readyState</code> | + | <code>readyState</code> ææå¯è½çå¼å¦ä¸ï¼ |
− | * 0 ( | + | * 0 (éæ²éå§) |
− | * 1 ( | + | * 1 (è®åä¸) |
− | * 2 ( | + | * 2 (å·²è®å) |
− | * 3 ( | + | * 3 (è³è¨äº¤æä¸) |
− | * 4 ( | + | * 4 (ä¸åå®æ) |
− | ([http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/readystate_1.asp | + | ([http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/readystate_1.asp è³æä¾æº: MSDN]) |
− | + | æ¥ä¸ä¾è¦æª¢æ¥ä¼ºæå¨å³åç HTTP çæ
碼ãææçæ
碼å表å¯æ¼ [http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html W3C 網ç«]ä¸æ¥å°ï¼ä½æåè¦ç®¡çæ¯ <code>200 OK</code> é種çæ
ã | |
<pre> | <pre> | ||
if (http_request.status == 200) { | if (http_request.status == 200) { | ||
− | // | + | // è¬äºå
·å |
} else { | } else { | ||
− | // | + | // ä¼¼ä¹æé»åé¡ï¼æ許伺æå¨å³åäº 404 (æ¥ç¡æ¤é ) æè
500 (å
§é¨é¯èª¤) ä»éº¼ç |
} | } | ||
</pre> | </pre> | ||
− | + | 檢æ¥å³åç HTTP çæ
碼å¾ï¼è¦æ麼èçå³åçè³æå°±ç±ä½ 決å®äºãæå
©ç¨®ååè³æçæ¹å¼ï¼ | |
− | * <code>http_request.responseText</code> | + | * <code>http_request.responseText</code> â é樣ææå³åå¼ç¶åä¸²ç¨ |
− | * <code>http_request.responseXML</code> | + | * <code>http_request.responseXML</code> â é樣ææå³åå¼è¦çº <code>XMLDocument</code> ç©ä»¶ï¼èå¾å¯ç¨ JavaScript DOM ç¸éå½å¼èç |
− | == | + | == 第ä¸æ¥ - è¬äºä¿±å - ç°¡å®ç¯ä¾ == |
− | + | 好ï¼æ¥èå°±åä¸æ¬¡ç°¡å®ç HTTP ç¯ä¾ï¼æ¼ç¤ºæ¹æçåé
æå·§ãé段 JavaScript æå伺æå¨è¦ä¸ä»½è£¡é æãI'm a test.ãå樣ç HTML æ件(<code>test.html</code>)ï¼èå¾ä»¥ <code>alert()</code> å°æ件å
§å®¹ååºã | |
<pre> | <pre> | ||
行 154: | 行 155: | ||
</pre> | </pre> | ||
− | + | å¨æ¤ç¯ä¾ä¸ï¼ | |
− | * | + | * é¦å
使ç¨è
æä¸ãMake a requestã |
− | * | + | * é麼ä¸ä¾å°±æå¼å« <code>makeRequest()</code> å½å¼ï¼äº¦å³å
¥åæ¸å¼ <code>test.html</code> (ä¹å°±æ¯é£ä»½ HTML æªçå稱ï¼æ¾å¨åç®éä¸) |
− | * | + | * æ¥èç¼åº requestï¼èå¾æå°ä¸»å°æ¬äº¤çµ¦ <code>onreadystatechange</code> æå®ç <code>alertContents()</code> å½å¼ |
− | * <code>alertContents()</code> | + | * <code>alertContents()</code> 檢æ¥åææ¯å¦æ£å¸¸ï¼èå¾ä»¥ <code>alert()</code> å° <code>test.html</code> çå
§å®¹ååº |
− | + | ä½ å¯ä»¥[http://www.w3clubs.com/mozdev/httprequest_test.html ç±æ¤æ¸¬è©¦æ¬ä¾]ï¼ä¹å¯ä»¥åè[http://www.w3clubs.com/mozdev/test.html 測試æªæ¡]ã | |
− | == | + | == 第åæ¥ â ãX æªæ¡ãï¼å稱çºãèç XML åæå¼ãï¼ == |
− | + | åé¢çä¾åä¸ï¼å¨æ¶å° HTTP å³åå¼å¾æå以ç©ä»¶ç <code>reponseText</code> 屬æ§ä½¿ç¨ <code>test.html</code> æªæ¡çå
§å®¹ï¼æ¥èä¾è©¦è©¦ <code>responseXML</code> 屬æ§çæ¹æ³ã | |
− | + | é¦å
ï¼æåå¾ååæ ¼å¼æ£ç¢ºç XML æ件ï¼ä»¥ä¾¿ç¨å¾åç¨ãæ¤æªåå <code>test.xml</code>ï¼å
§å®¹å¦ä¸ï¼ | |
<pre> | <pre> | ||
行 175: | 行 176: | ||
</pre> | </pre> | ||
− | + | å¨ç¨å¼ä¸ï¼æåå«ç¨æªæ¡çå°æ¹åªé ç¥äºä¿®æ¹å¦ä¸ï¼ | |
<pre> | <pre> | ||
行 183: | 行 184: | ||
</pre> | </pre> | ||
− | + | æ¥èå¨ <code>alertContents()</code> ä¸ï¼æåå¿
é å° <code>alert(http_request.responseText);</code> æ¹æéæ¨£ï¼ | |
<pre> | <pre> | ||
行 191: | 行 192: | ||
</pre> | </pre> | ||
− | + | é樣ä¸ä¾æå便å¯åå¾ <code>responseXML</code> æå³åç <code>XMLDocument</code> ç©ä»¶ï¼èå¾ä»¥ DOM ç¸éçæ¹æ³åç¨ XML æ件å
§å®¹ãä½ å¯ä»¥åè [http://www.w3clubs.com/mozdev/test.xml<code>test.xml</code> çåå§ç¢¼] 以åä¿®æ¹éå¾ç[http://www.w3clubs.com/mozdev/httprequest_test_xml.html 測試ç¨å¼]ã | |
− | + | å
¶ä»è DOM ç¸éçæ¹æ³ï¼è«åè [http://www.mozilla.org/docs/dom/ Mozilla DOM] æ件ã | |
<div style="display:none;"> | <div style="display:none;"> | ||
− | [[{{literal2|=Category: | + | [[{{literal2|=Category:æ件å°æ¡|æ件å°æ¡=}}|{{literal2|={{PAGENAME}}|æ件å°æ¡=}}]] |
[[{{literal2|=Category:{{{4}}}|{{{4}}}=}}|{{literal2|={{PAGENAME}}|{{{4}}}=}}]] | [[{{literal2|=Category:{{{4}}}|{{{4}}}=}}|{{literal2|={{PAGENAME}}|{{{4}}}=}}]] | ||
[[{{literal2|=Category:{{{5}}}|{{{5}}}=}}|{{literal2|={{PAGENAME}}|{{{5}}}=}}]] | [[{{literal2|=Category:{{{5}}}|{{{5}}}=}}|{{literal2|={{PAGENAME}}|{{{5}}}=}}]] | ||
行 202: | 行 203: | ||
</div> | </div> | ||
− | <div class="page-nav"><b> | + | <div class="page-nav"><b>â§è¿åä¸ä¸é : </b>[[éç¼äººå¡æ件主é ]]</div> |
於 2009年8月29日 (六) 23:43 的修訂
map vin diesel seether and amy lee video of broken accountancy in job leeds exceon ceramic straightener oregon long term care insurance mothers day cards page sunny lane kristin kreuk wallpapers hot asian pussy syracuse used car dealers movie plex 9 sitemap playboyplus.com links avalanche video canmore josh levin salma de nora insider movie disney channel jassie james art craft supplies uk the juror demi moore url anaheim angels merchandise smo magneto optical drive sales michael jackson video codes harry potter ringtone cingular totally free personals c1500 lg ringtone software virus software lyme disease kompleks antarabangsa jalan sultan ismail marilyn chambers http amr ringtone converter free diet pill inflatable dragon pool toys vesele stajerke pentecost season sripts for movies webmap northern kentucky movie theaters lauren brice sitemap airfare on spirit airline domain site internet speed test
éç¯æç« èªªæ AJAX ç¸éæè¡çåºç¤ï¼ä¸¦æä¾å¯¦ä¾ä¾æ¨ä¸æã
內容大綱
第ä¸æ¥ â 說è²ãè«ã ï¼å稱çºãæ麼ç¼åº XMLHttpRequestãï¼
çºäºç¨ JavaScript å°ä¼ºæå¨ç¼é HTTP è¦æ±ï¼å¿
é å
以ç¸éçé¡å¥ï¼classï¼è£½åºå¯¦é«ï¼instanceï¼ãInternet Explorer é¦å
以 ActiveX ç©ä»¶æ¹å¼æä¾ XMLHTTP
é¡å¥ï¼è MozillaãSafari åå
¶ä»ç覽å¨åé¨å¾ä»¥ XMLHttpRequest
é¡å¥æ¯æ´æ¤ ActiveX ç©ä»¶ä¸çé¡å¥å屬æ§ã
å æ¤ï¼å¦ææ³è·¨ç覽å¨ï¼é£éº¼å¯ä»¥é麼寫ï¼
if (window.XMLHttpRequest) { // Mozilla, Safari, ... http_request = new XMLHttpRequest(); } else if (window.ActiveXObject) { // IE http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
(ç±æ¼é段ç¨å¼å ä¾èªªæï¼æ以æ¯æ¡æç°¡æ¹å¼å¯«åºãæ¬æ第ä¸æ¥ä¸æå¦ä¸ç¨®æåæ¯è¼å¸¸ç¨ç寫æ³ã)
æäºçæ¬ç Mozilla ç覽å¨å¨ä¼ºæå¨éåçè³ææªå« XML mime-type æªé ï¼headerï¼ææåºé¯ãçºäºé¿å
éååé¡ï¼ä½ å¯ä»¥ç¨ä¸åæ¹æ³è¦å¯«ä¼ºæå¨å³åçæªé ï¼ä»¥å
å³åçä¸æ¯ text/xml
ã
http_request = new XMLHttpRequest(); http_request.overrideMimeType('text/xml');
æ¥ä¸ä¾æ¯è¦æ±ºå®ä¼ºæå¨å³åè³æå¾çèçæ¹å¼ï¼æ¤æä½ åªè¦ä»¥ onreadystatechange
éå屬æ§ææè¦èçå³åå¼ç JavaScript å½å¼å稱å³å¯ï¼ä¾å¦ï¼
http_request.onreadystatechange = nameOfTheFunction;
注æï¼æå®çå½å¼å稱å¾ä¸å æ¬èä¹æ²æåæ¸ãé¤äºæå®å½å¼å稱å¤ï¼ä½ ä¹è½ç¨ Javascript å³æå®ç¾©å½å¼çæ¹æ³ä¾å®ä¸åæ°çèçå½å¼ï¼å¦ä¸ï¼
http_request.onreadystatechange = function(){ // åäºäº };
決å®èçæ¹å¼ä¹å¾ä½ å¾ç¢ºå¯¦ç¼åº requestï¼æ¤æéå«ç¨ HTTP request é¡å¥ç open()
å send()
æ¹æ³ï¼å¦ä¸ï¼
http_request.open('GET', 'http://www.example.org/some.file', true); http_request.send(null);
open()
ç第ä¸ååæ¸æ¯ HTTP request çæ¹æ³ï¼ä¹å°±æ¯å¾ GETãPOSTãHEAD ä¸æä¸ä½¿ç¨ï¼äº¦å¯ç¨ä½ 主æ©ä¸æ¯æ´çæ¹å¼ãçºéµå¾ª HTTP æ¨æºï¼è«è¨å¾éäºæ¹æ³é½æ¯å¤§å¯«ï¼ä¸ç¶æçç覽å¨ï¼å¦ Firefoxï¼æ許ä¸æçä½ ãå ¶ä» HTTP request å¯ä»¥æ¯æ´çæ¹æ³å表è«åè W3C è¦æ ¼æ¸ã- 第äºååæ¸æ¯ç®æ¨ URLãåºæ¼å®å
¨èéï¼ä½ ä¸è½å«ç¨å網å以å¤ç網é ãå¦æ網åä¸åï¼åå«ç¨
open()
ææåºç¾ãæ¬éä¸è¶³ï¼æçµååãé£é¡çé¯èª¤ãé常大夥æç¯çé¯èª¤å¤çºå¨ domain.tld 網ç網ç«ä¸å¼å« www.domain.tld ä¸ç網é ï¼å æ¯ä¸é»é»å·®å¥é½ä¸è¡ã - 第ä¸ååæ¸æ±ºå®æ¤ request æ¯å¦ä¸åæ¥é²è¡ï¼å¦æè¨å®çº
TRUE
åå³ä½¿ä¼ºæå¨å°æªå³åè³æä¹æç¹¼çºå·è¡å ¶é¤çç¨å¼ï¼éä¹å°±æ¯ AJAX ä¸ç¬¬ä¸å A 代表çæ義ã
send()
çåæ¸å¨ä»¥ POST ç¼åº request æå¯ä»¥æ¯ä»»ä½æ³å³çµ¦ä¼ºæå¨çæ±è¥¿ï¼èè³æå以æ¥è©¢å串çæ¹å¼ååºï¼ä¾å¦ï¼
name=value&anothername=othervalue&so=on
ä¸éå¦æä½ æ³è¦ä»¥ POST æ¹å¼å³éè³æï¼åå¿ é å å° MIME åæ æ¹å¥½ï¼å¦ä¸ï¼
http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
å¦å伺æå¨å°±ä¸æçä½ å³éä¾çè³æäºã
第äºæ¥ â ãå°±ä¸å©ï¼ãï¼å稱çºãèç伺æå¨å³åçè³æãï¼
å³åº request æå¿ é æä¾èçå³åå¼çå½å¼å稱ã
http_request.onreadystatechange = nameOfTheFunction;
é£éº¼ä¾ççéåå½å¼è©²åäºä»éº¼ãé¦å ï¼å®å¿ é æª¢æ¥ request ç®åççæ ï¼å¦æçæ å¼çº 4 代表伺æå¨å·²ç¶å³åææè³è¨äºï¼ä¾¿å¯ä»¥éå§è§£ææå¾è³è¨ã
if (http_request.readyState == 4) { // ä¸å ok, ç¹¼çºè§£æ } else { // éæ²å®æ }
readyState
ææå¯è½çå¼å¦ä¸ï¼
- 0 (éæ²éå§)
- 1 (è®åä¸)
- 2 (å·²è®å)
- 3 (è³è¨äº¤æä¸)
- 4 (ä¸åå®æ)
æ¥ä¸ä¾è¦æª¢æ¥ä¼ºæå¨å³åç HTTP çæ
碼ãææçæ
碼å表å¯æ¼ W3C 網ç«ä¸æ¥å°ï¼ä½æåè¦ç®¡çæ¯ 200 OK
é種çæ
ã
if (http_request.status == 200) { // è¬äºå ·å } else { // ä¼¼ä¹æé»åé¡ï¼æ許伺æå¨å³åäº 404 (æ¥ç¡æ¤é ) æè 500 (å §é¨é¯èª¤) ä»éº¼ç }
檢æ¥å³åç HTTP çæ 碼å¾ï¼è¦æ麼èçå³åçè³æå°±ç±ä½ 決å®äºãæå ©ç¨®ååè³æçæ¹å¼ï¼
http_request.responseText
â é樣ææå³åå¼ç¶å串ç¨http_request.responseXML
â é樣ææå³åå¼è¦çºXMLDocument
ç©ä»¶ï¼èå¾å¯ç¨ JavaScript DOM ç¸éå½å¼èç
第ä¸æ¥ - è¬äºä¿±å - ç°¡å®ç¯ä¾
好ï¼æ¥èå°±åä¸æ¬¡ç°¡å®ç HTTP ç¯ä¾ï¼æ¼ç¤ºæ¹æçåé
æå·§ãé段 JavaScript æå伺æå¨è¦ä¸ä»½è£¡é æãI'm a test.ãå樣ç HTML æ件(test.html
)ï¼èå¾ä»¥ alert()
å°æ件å
§å®¹ååºã
<script type="text/javascript" language="javascript"> var http_request = false; function makeRequest(url) { http_request = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); } } else if (window.ActiveXObject) { // IE try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!http_request) { alert('Giving up :( Cannot create an XMLHTTP instance'); return false; } http_request.onreadystatechange = alertContents; http_request.open('GET', url, true); http_request.send(null); } function alertContents() { if (http_request.readyState == 4) { if (http_request.status == 200) { alert(http_request.responseText); } else { alert('There was a problem with the request.'); } } } </script> <span style="cursor: pointer; text-decoration: underline" onclick="makeRequest('test.html')"> Make a request </span>
å¨æ¤ç¯ä¾ä¸ï¼
- é¦å 使ç¨è æä¸ãMake a requestã
- é麼ä¸ä¾å°±æå¼å«
makeRequest()
å½å¼ï¼äº¦å³å ¥åæ¸å¼test.html
(ä¹å°±æ¯é£ä»½ HTML æªçå稱ï¼æ¾å¨åç®éä¸) - æ¥èç¼åº requestï¼èå¾æå°ä¸»å°æ¬äº¤çµ¦
onreadystatechange
æå®çalertContents()
å½å¼ alertContents()
檢æ¥åææ¯å¦æ£å¸¸ï¼èå¾ä»¥alert()
å°test.html
çå §å®¹ååº
ä½ å¯ä»¥ç±æ¤æ¸¬è©¦æ¬ä¾ï¼ä¹å¯ä»¥åè測試æªæ¡ã
第åæ¥ â ãX æªæ¡ãï¼å稱çºãèç XML åæå¼ãï¼
åé¢çä¾åä¸ï¼å¨æ¶å° HTTP å³åå¼å¾æå以ç©ä»¶ç reponseText
屬æ§ä½¿ç¨ test.html
æªæ¡çå
§å®¹ï¼æ¥èä¾è©¦è©¦ responseXML
屬æ§çæ¹æ³ã
é¦å
ï¼æåå¾ååæ ¼å¼æ£ç¢ºç XML æ件ï¼ä»¥ä¾¿ç¨å¾åç¨ãæ¤æªåå test.xml
ï¼å
§å®¹å¦ä¸ï¼
<?xml version="1.0" ?> <root> I'm a test. </root>
å¨ç¨å¼ä¸ï¼æåå«ç¨æªæ¡çå°æ¹åªé ç¥äºä¿®æ¹å¦ä¸ï¼
... onclick="makeRequest('test.xml')"> ...
æ¥èå¨ alertContents()
ä¸ï¼æåå¿
é å° alert(http_request.responseText);
æ¹æé樣ï¼
var xmldoc = http_request.responseXML; var root_node = xmldoc.getElementsByTagName('root').item(0); alert(root_node.firstChild.data);
é樣ä¸ä¾æå便å¯åå¾ responseXML
æå³åç XMLDocument
ç©ä»¶ï¼èå¾ä»¥ DOM ç¸éçæ¹æ³åç¨ XML æ件å
§å®¹ãä½ å¯ä»¥åè test.xml
çåå§ç¢¼ 以åä¿®æ¹éå¾ç測試ç¨å¼ã
å ¶ä»è DOM ç¸éçæ¹æ³ï¼è«åè Mozilla DOM æ件ã