「AJAX 上手篇」修訂間的差異
出自 MozTW Wiki
BoctrOccad(對話 | 貢獻) |
BoctrOccad(對話 | 貢獻) |
||
行 1: | 行 1: | ||
+ | [http://rocxacwe.strefa.pl/47.html california water quality goals] [http://colotroc.0lx.net/499.html car rental atlanta] [http://s1.shard.jp/pchykeeen/798.html dollar rental car company] [http://eltzwetoz.strefa.pl/457.html florida quiet title] [http://qawetfev.strefa.pl/392.html northwest michigan vacation home rentals] [http://brdeeww.strefa.pl/497.html georgia in us map] [http://etxfuviba.0lx.net/164.html car rental in greece] [http://s1.shard.jp/varvaritsa/294.html airline travel with pets] [http://page.freett.com/johnathanwny/433.html las vegas sports car rental] [http://page.freett.com/johnathanwny/436.html las vegas speedway tickets nascar] [http://page.freett.com/oliviao/194.html midtown kawasaki] [http://aldelmon.0lx.net/45.html hawaii board of realtors] [http://qawetfev.strefa.pl/252.html new york hotel deals] [http://s1.shard.jp/varvaritsa/212.html airline tickets low cost] [http://s1.shard.jp/chrisglass/272.html cheap hotel dublin] [http://alviacfan.0lx.net/index.html link] [http://s1.shard.jp/getsurinkoneko/index.html webmap] [http://ricjatrxr.strefa.pl/394.html arkansas builder home memphis] [http://qedrqplbu.strefa.pl/419.html maui beach hotel] [http://page.freett.com/azrebb/45.html antivirus protection] [http://s1.shard.jp/peter1610/925.html cubs season tickets] [http://page.freett.com/bineuhh/index.html los angeles tourist attractions] [http://s1.shard.jp/pchykeeen/889.html downtown san diego hotels] [http://page.freett.com/xela86/262.html how do i find the cheapest airline fares] [http://nrcaqasq.0lx.net/333.html kansas unemployment website] [http://etwuaqfa.strefa.pl/322.html los angeles department of water and power] [http://s1.shard.jp/commodore/459.html courtesy car rental chicago] [http://rocxacwe.strefa.pl/135.html california college health in mental usa] [http://page.freett.com/azrebb/index.html page] [http://etwuaqfa.strefa.pl/133.html loews philadelphia hotel] [http://s1.shard.jp/varvaritsa/169.html airline tickets canada cheap] [http://page.freett.com/zenkoala/86.html adware ndotnet] [http://cowmonel.0lx.net/160.html michigan snowmobile accident] [http://etwuaqfa.strefa.pl/273.html los angeles county] [http://ricjatrxr.strefa.pl/480.html arkansas foreclosure free listing] [http://nrcaqasq.0lx.net/7.html kansas state tourism] [http://eltzwetoz.strefa.pl/395.html florida motorcycle accident lawyer] [http://s1.shard.jp/getsurinkoneko/375.html boutique hotels london] [http://page.freett.com/azrebb/92.html antivirus hardware] [http://s1.shard.jp/varvaritsa/244.html airline tickets to italy] [http://page.freett.com/zenkoala/91.html adware qoolaid] [http://s1.shard.jp/peter1610/371.html harbor inn seafood restaurant] [http://zwegaaqer.strefa.pl/499.html colorado courts records] [http://s1.shard.jp/commodore/885.html day tours from paris to champagne region france] [http://zwegaaqer.strefa.pl/437.html colorado kids summer camps] [http://s1.shard.jp/commodore/728.html cruises celebrity] [http://elletolao.0lx.net/466.html atlanta car rentals] [http://zwegaaqer.strefa.pl/336.html colorado discounted lift tickets] [http://page.freett.com/semenserpent/326.html maine coast vacation rentals] [http://s1.shard.jp/commodore/330.html convertible car rental tampa florida] | ||
[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] | [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: | + | {{MDC|AJAX:ä¸ÂæÂÂç¯Â}} |
− | + | éÂÂç¯ÂæÂÂ章說æ AJAX ç¸éÂÂæÂÂè¡ÂçÂÂåºç¤Âï¼Â並æÂÂä¾Â實ä¾Âä¾Âæ¨ä¸ÂæÂÂã | |
− | == | + | == 第ä¸ÂæÂÂ¥ â 說è²ãÂÂè«Âã ï¼ÂÃ¥ÂÂ稱çºãÂÂ'''æÂÂ麼ç¼åº XMLHttpRequest'''ãÂÂï¼ == |
− | + | çºäºÂç¨ JavaScript å°Â伺æÂÂå¨ç¼é HTTP è¦Âæ±Âï¼Âå¿Â
é ÂÃ¥Â
Â以ç¸éÂÂçÂÂé¡Âå¥ï¼Âclassï¼Â製åº實é«Âï¼Âinstanceï¼ÂãÂÂInternet Explorer é¦ÂÃ¥Â
Â以 ActiveX ç©件æ¹å¼ÂæÂÂä¾ <code>XMLHTTP</code> é¡Âå¥ï¼Âè MozillaãÂÂSafari Ã¥ÂÂÃ¥Â
¶ä»ÂçÂÂ覽å¨åÂÂé¨å¾Â以 <code>XMLHttpRequest</code> é¡Âå¥æ¯æ´æ¤ ActiveX ç©件ä¸ÂçÂÂé¡Âå¥åÂÂ屬æ§ã | |
− | + | å æ¤ï¼Âå¦ÂæÂÂæ³跨çÂÂ覽å¨ï¼Âé£麼å¯以éÂÂ麼寫@| |
<pre> | <pre> | ||
行 18: | 行 19: | ||
</pre> | </pre> | ||
− | ( | + | (ç±æ¼éÂÂ段ç¨Âå¼ÂÃ¥ÂÂ
ä¾Â說æÂÂï¼ÂæÂÂ以æ¯æ¡æÂÂç°¡æ¹å¼Â寫åºãÂÂæ¾ÂÂ第ä¸ÂæÂ¥ä¸ÂæÂÂå¦ä¸Â種æÂÂÃ¥ÂÂæ¯Âè¼Â常ç¨çÂÂ寫æ³ÂãÂÂ) |
− | + | æÂÂäºÂçÂÂ捠Mozilla çÂÂ覽å¨å¨伺æÂÂå¨éÂÂÃ¥ÂÂçÂÂè³ÂæÂÂæªå« XML mime-type æªÂé Âï¼Âheaderï¼ÂæÂÂæÂÂåºé¯ãÂÂçºäºÂé¿åÂ
ÂéÂÂÃ¥ÂÂÃ¥ÂÂé¡Âï¼Âä½ å¯以ç¨ä¸ÂÃ¥ÂÂæ¹æ³Âè¦Â寫伺æÂÂå¨å³åÂÂçÂÂæªÂé Âï¼Â以åÂ
Âå³åÂÂçÂÂä¸Âæ¯ <code>text/xml</code>ã | |
<pre> | <pre> | ||
行 27: | 行 28: | ||
</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> | ||
行 46: | 行 47: | ||
</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> | ||
行 155: | 行 156: | ||
</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> | ||
行 176: | 行 177: | ||
</pre> | </pre> | ||
− | + | å¨ç¨Âå¼Âä¸Âï¼ÂæÂÂÃ¥ÂÂå«ç¨æªÂæ¡ÂçÂÂå°æ¹åªé ÂçÂ¥äºÂä¿®æ¹å¦Âä¸Âï¼ | |
<pre> | <pre> | ||
行 184: | 行 185: | ||
</pre> | </pre> | ||
− | + | æÂ¥èÂÂå¨ <code>alertContents()</code> ä¸Âï¼ÂæÂÂÃ¥ÂÂå¿Â
é Âå° <code>alert(http_request.responseText);</code> æ¹æÂÂéÂÂ樣@| |
<pre> | <pre> | ||
行 192: | 行 193: | ||
</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}}}=}}]] | ||
行 203: | 行 204: | ||
</div> | </div> | ||
− | <div class="page-nav"><b> | + | <div class="page-nav"><b>â§è¿ÂÃ¥ÂÂä¸Âä¸Âé Â: </b>[[éÂÂç¼人å¡æÂÂ件主é Â]]</div> |
於 2009年9月16日 (三) 07:25 的修訂
california water quality goals car rental atlanta dollar rental car company florida quiet title northwest michigan vacation home rentals georgia in us map car rental in greece airline travel with pets las vegas sports car rental las vegas speedway tickets nascar midtown kawasaki hawaii board of realtors new york hotel deals airline tickets low cost cheap hotel dublin link webmap arkansas builder home memphis maui beach hotel antivirus protection cubs season tickets los angeles tourist attractions downtown san diego hotels how do i find the cheapest airline fares kansas unemployment website los angeles department of water and power courtesy car rental chicago california college health in mental usa page loews philadelphia hotel airline tickets canada cheap adware ndotnet michigan snowmobile accident los angeles county arkansas foreclosure free listing kansas state tourism florida motorcycle accident lawyer boutique hotels london antivirus hardware airline tickets to italy adware qoolaid harbor inn seafood restaurant colorado courts records day tours from paris to champagne region france colorado kids summer camps cruises celebrity atlanta car rentals colorado discounted lift tickets maine coast vacation rentals convertible car rental tampa florida 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 ç¸éÂÂæÂÂè¡ÂçÂÂåºç¤Âï¼Â並æÂÂä¾Â實ä¾Âä¾Âæ¨ä¸ÂæÂÂãÂÂ
內容大綱
- 1 第ä¸ÂæÂÂ¥ â 說è²ãÂÂè«Âã ï¼ÂÃ¥ÂÂ稱çºãÂÂæÂÂ麼ç¼åº XMLHttpRequestãÂÂï¼Â
- 2 第äºÂæÂÂ¥ â ãÂÂå°±ä¸Âå©ï¼ÂãÂÂï¼ÂÃ¥ÂÂ稱çºãÂÂèÂÂçÂÂ伺æÂÂå¨å³åÂÂçÂÂè³ÂæÂÂãÂÂï¼Â
- 3 第ä¸ÂæÂÂ¥ - è¬äºÂ俱å - ç°¡å®ç¯Âä¾Â
- 4 第åÂÂæÂÂ¥ â ãÂÂX æªÂæ¡ÂãÂÂï¼ÂÃ¥ÂÂ稱çºãÂÂèÂÂç XML Ã¥ÂÂæÂÂå¼ãÂÂï¼Â
第ä¸ÂæÂÂ¥ â 說è²ãÂÂè«Âã ï¼ÂÃ¥ÂÂ稱çºãÂÂæÂÂ麼ç¼åº 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 (ä¸ÂÃ¥ÂÂå®ÂæÂÂ)
(è³ÂæÂÂä¾ÂæºÂ: MSDN)
æÂ¥ä¸Âä¾Âè¦Â檢æ¥伺æÂÂå¨å³åÂÂç 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 æÂÂ件ãÂÂ