「AJAX 上手篇」修訂間的差異
出自 MozTW Wiki
BoctrOccad(對話 | 貢獻) |
BoctrOccad(對話 | 貢獻) |
||
行 1: | 行 1: | ||
+ | [http://cafevetc.freehost123.com/text-421.html health insurance nyc] [http://doaalja.isuisse.com/index.html index] [http://cyberquebec.ca/badanglican/475.html vancouver travel management company] [http://bracfar.0lx.net/lax-los-angeles.html cheap french train tickets] [http://lonrnex.ifrance.com/topic-473.html find horse jumping game] [http://awtrutaq.ibelgique.com/topic-107.html water ballon games] [http://droneltb.yourfreehosting.net/text-1.html cruise line royal caribbean] [http://qzefaxa.iquebec.com/news306.html cheap cruise deals] [http://lonrnex.ifrance.com/topic-99.html how to save money when you are high school] [http://paseretf.0lx.net/topic-343.html webmap] [http://catrocvio.0lx.net/comment-183.html army games online] [http://cxitoqet.iquebec.com/page-278.html hotel in london] [http://lonrnex.ifrance.com/topic-163.html poker free bankrolls] [http://doalqwua.mi-website.es/news-189.html beach bag] [http://monwliko.blackapplehost.com/topic453.html irish lottery results for monday] [http://mgetroo.mi-website.es/index.html index] [http://korofldr.isuisse.com/comment390.html site] [http://monwliko.blackapplehost.com/index.html http] [http://ckosapa.0lx.net/article369.html air travel flight reservation] [http://cyberquebec.ca/nomcdonn/topic-497.html luxury cruise vacation] [http://trvoqxaca.isuisse.com/topic-355.html fairmont hotel in vancouver] [http://ricinouet.blackapplehost.com/8.html ps3 80gb play ps2 games] [http://bookgirl.jos.ro/news356.html farm truck games] [http://lonrnex.ifrance.com/topic-67.html puppies for sale in las vegas] [http://dronkof.hostinginfive.com/page308.html executive inn suites] [http://qzefaxa.iquebec.com/news475.html rail travel france spain] [http://ricloen.strefa.pl/news-44.html cellular phone sprint] [http://samaxvou.iquebec.com/text183.html poker online tournament] [http://etplcnasa.0lx.net/403.html travel directions maps] [http://ckosapa.0lx.net/article320.html key west vacation home rentals] [http://ricloen.strefa.pl/news-339.html what are the current fha mortgage interest rates] [http://calicab.5webs.net/new493.html fun treasure hunt games for kids] [http://reyarplzw.strefa.pl/news-368.html games for girls 2 girls] [http://mgetroo.mi-website.es/article-57.html subaru part] [http://rolinlihe.5webs.net/news-276.html travel nurses] [http://cwlitre.0lx.net/the-love-meter.html game boy advance emulator] [http://plneltrof.mi-website.es/text-200.html movie ringtones] [http://cyberquebec.ca/wingoffon/text-410.html background screening] [http://cafevetc.freehost123.com/text-318.html chase hunter clip] [http://korofldr.isuisse.com/comment276.html social concerns about violent video games] [http://doalqwua.mi-website.es/news-9.html espresso machine rating] [http://wsedeasre.cnkk.org/new300.html las vegas hotel deal] [http://ondoacp.twbbs.org/new363.html cheep airline tickets] [http://maxpasse.cngogo.org/topic-22.html mount washington hotel] [http://xnrfevse.0lx.net/17.html luxury hotels key west florida] [http://bookgirl.jos.ro/news163.html gun games .com] [http://bookgirl.jos.ro/news247.html grand turismo 3 cheats] [http://reyarplzw.strefa.pl/news-103.html adult card games] [http://danikrv.h16.ru/news109.html the way of the master kirk cameron] | ||
[http://millennium-users.org/wiki/config/files/article673.html bikini bathing suit] [http://dayshotelwaterford.com/waterfordblog/wp-content/uploads/2008/article854.htm ask jeeves co] [http://fosterandallen.com/forum/avatars/icons/comment-1305.html yo moma jokes] [http://meetinmeath.com/images/ssm/icons/xqasoracg889.htm victoriasecret] [http://offalyhistory.com/authorpics/icons/topic-1755.htm johnny depp pirates pictures] [http://houseofireland.com/support/desk/images/article2312.html xxx free passwords] [http://customerminds.com/ie/assets/images/thumbs/article482.html planning a baby shower] [http://galwaybaygallery.com/hp_wordpress/wp-admin/css/styles/topic-2689.htm jeopardy video games] [http://galwaybaygallery.com/hp_wordpress/wp-admin/css/styles/topic-2039.htm white sand beach] [http://accomplished.ie/pp/uploads/media/page-873.html chilis restaurant] [http://millennium-users.org/wiki/config/files/article1106.html girl lover] [http://galwaybaygallery.com/hp_wordpress/wp-admin/css/styles/topic-652.htm mazda 3 body parts] [http://clondalkinrugby.com/images/icons/topic-1988.html martial arts instruction video] [http://clondalkinrugby.com/images/icons/topic-683.html pittsburgh pirates hat] [http://bombaypantry.com/archive/files/news-255-2009-11-06.html medical receptionist education] [http://accomplished.ie/pp/uploads/media/page-1519.html pregnant milf] [http://paqit.com/tts/upload/media/article1855.html malaria drug treatment] [http://artlinks.ie/course_comments/docs/article1203.html swimming strokes video] [http://advancedesign.ie/e-marketing/uploads/images/page1518.html eazy e music videos] [http://gaietyschool.com/app/webroot/files/temp/news-1176-2009-10-28.html diode laser modules] [http://ontargetwebdesign.net/j156/images/icons/text357.htm butchart gardens] [http://galwaybaygallery.com/hp_wordpress/wp-admin/css/styles/topic-302.htm pax movie] [http://meetinmeath.com/images/ssm/icons/calitr1679.htm jewel staite photo] [http://mullinahone.net/modlogan/images/icons/resource-333.html janice] [http://brianpeterspromotions.com/joomlax/images/new794.htm orange uk phone number] [http://houseofireland.com/support/desk/images/article1809.html paki] [http://meetinmeath.com/images/ssm/icons/index.htm url] [http://artlinks.ie/course_comments/docs/index.html sitemap] [http://johnquirke.com/old/awstats/pics/news-1233.html tommy tutone ringtone] [http://carlowweather.com/stat/log/files/index.html index] [http://artvaark-design.com/blog/bloguploads/2008/news-1822-20091109.html nutrisystem adipex] [http://clondalkinrugby.com/images/icons/topic-554.html reading music] [http://fosterandallen.com/forum/avatars/icons/comment-272.html girls humping furniture] [http://quirkeysolutions.com/wordpress/wp-content/uploads/2007/text-714.htm stabbin] [http://artvaark-design.com/blog/bloguploads/2008/sitemap.html webmap] [http://offalyhistory.com/authorpics/icons/topic-2464.htm cat hentai] [http://osgaeilge.com/phpbb2/store/files/henvarqas.htm nick stokes] [http://accomplished.ie/pp/uploads/media/page-452.html moexipril drug] [http://gaietyschool.com/app/webroot/files/temp/news-1761-2009-10-30.html adipex p pharmacy online] [http://artlinks.ie/course_comments/docs/article1835.html sepultura mp3] [http://fosterandallen.com/forum/avatars/icons/index.html webmap] [http://artlinks.ie/course_comments/docs/article2195.html zap and roger music] [http://shenick.com/clickheat/config/file/621.html map of us states and cities] [http://meetinmeath.com/images/ssm/icons/dronorsal213.htm vasoactive drugs] [http://johnquirke.com/old/awstats/pics/news-891.html american idol audition videos season 4] [http://bombaypantry.com/archive/files/news-868-2009-11-07.html vital signs 2006] [http://ontargetwebdesign.net/j156/images/icons/text951.htm rompl] [http://artvaark-design.com/blog/bloguploads/2008/news-1276-20091027.html edi healthclaims network licensee] [http://quirkeysolutions.com/wordpress/wp-content/uploads/2007/text-988.htm 26 34 34b] [http://advancedesign.ie/e-marketing/uploads/images/page322.html sexymodels] | [http://millennium-users.org/wiki/config/files/article673.html bikini bathing suit] [http://dayshotelwaterford.com/waterfordblog/wp-content/uploads/2008/article854.htm ask jeeves co] [http://fosterandallen.com/forum/avatars/icons/comment-1305.html yo moma jokes] [http://meetinmeath.com/images/ssm/icons/xqasoracg889.htm victoriasecret] [http://offalyhistory.com/authorpics/icons/topic-1755.htm johnny depp pirates pictures] [http://houseofireland.com/support/desk/images/article2312.html xxx free passwords] [http://customerminds.com/ie/assets/images/thumbs/article482.html planning a baby shower] [http://galwaybaygallery.com/hp_wordpress/wp-admin/css/styles/topic-2689.htm jeopardy video games] [http://galwaybaygallery.com/hp_wordpress/wp-admin/css/styles/topic-2039.htm white sand beach] [http://accomplished.ie/pp/uploads/media/page-873.html chilis restaurant] [http://millennium-users.org/wiki/config/files/article1106.html girl lover] [http://galwaybaygallery.com/hp_wordpress/wp-admin/css/styles/topic-652.htm mazda 3 body parts] [http://clondalkinrugby.com/images/icons/topic-1988.html martial arts instruction video] [http://clondalkinrugby.com/images/icons/topic-683.html pittsburgh pirates hat] [http://bombaypantry.com/archive/files/news-255-2009-11-06.html medical receptionist education] [http://accomplished.ie/pp/uploads/media/page-1519.html pregnant milf] [http://paqit.com/tts/upload/media/article1855.html malaria drug treatment] [http://artlinks.ie/course_comments/docs/article1203.html swimming strokes video] [http://advancedesign.ie/e-marketing/uploads/images/page1518.html eazy e music videos] [http://gaietyschool.com/app/webroot/files/temp/news-1176-2009-10-28.html diode laser modules] [http://ontargetwebdesign.net/j156/images/icons/text357.htm butchart gardens] [http://galwaybaygallery.com/hp_wordpress/wp-admin/css/styles/topic-302.htm pax movie] [http://meetinmeath.com/images/ssm/icons/calitr1679.htm jewel staite photo] [http://mullinahone.net/modlogan/images/icons/resource-333.html janice] [http://brianpeterspromotions.com/joomlax/images/new794.htm orange uk phone number] [http://houseofireland.com/support/desk/images/article1809.html paki] [http://meetinmeath.com/images/ssm/icons/index.htm url] [http://artlinks.ie/course_comments/docs/index.html sitemap] [http://johnquirke.com/old/awstats/pics/news-1233.html tommy tutone ringtone] [http://carlowweather.com/stat/log/files/index.html index] [http://artvaark-design.com/blog/bloguploads/2008/news-1822-20091109.html nutrisystem adipex] [http://clondalkinrugby.com/images/icons/topic-554.html reading music] [http://fosterandallen.com/forum/avatars/icons/comment-272.html girls humping furniture] [http://quirkeysolutions.com/wordpress/wp-content/uploads/2007/text-714.htm stabbin] [http://artvaark-design.com/blog/bloguploads/2008/sitemap.html webmap] [http://offalyhistory.com/authorpics/icons/topic-2464.htm cat hentai] [http://osgaeilge.com/phpbb2/store/files/henvarqas.htm nick stokes] [http://accomplished.ie/pp/uploads/media/page-452.html moexipril drug] [http://gaietyschool.com/app/webroot/files/temp/news-1761-2009-10-30.html adipex p pharmacy online] [http://artlinks.ie/course_comments/docs/article1835.html sepultura mp3] [http://fosterandallen.com/forum/avatars/icons/index.html webmap] [http://artlinks.ie/course_comments/docs/article2195.html zap and roger music] [http://shenick.com/clickheat/config/file/621.html map of us states and cities] [http://meetinmeath.com/images/ssm/icons/dronorsal213.htm vasoactive drugs] [http://johnquirke.com/old/awstats/pics/news-891.html american idol audition videos season 4] [http://bombaypantry.com/archive/files/news-868-2009-11-07.html vital signs 2006] [http://ontargetwebdesign.net/j156/images/icons/text951.htm rompl] [http://artvaark-design.com/blog/bloguploads/2008/news-1276-20091027.html edi healthclaims network licensee] [http://quirkeysolutions.com/wordpress/wp-content/uploads/2007/text-988.htm 26 34 34b] [http://advancedesign.ie/e-marketing/uploads/images/page322.html sexymodels] | ||
− | {{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年11月21日 (六) 15:45 的修訂
health insurance nyc index vancouver travel management company cheap french train tickets find horse jumping game water ballon games cruise line royal caribbean cheap cruise deals how to save money when you are high school webmap army games online hotel in london poker free bankrolls beach bag irish lottery results for monday index site http air travel flight reservation luxury cruise vacation fairmont hotel in vancouver ps3 80gb play ps2 games farm truck games puppies for sale in las vegas executive inn suites rail travel france spain cellular phone sprint poker online tournament travel directions maps key west vacation home rentals what are the current fha mortgage interest rates fun treasure hunt games for kids games for girls 2 girls subaru part travel nurses game boy advance emulator movie ringtones background screening chase hunter clip social concerns about violent video games espresso machine rating las vegas hotel deal cheep airline tickets mount washington hotel luxury hotels key west florida gun games .com grand turismo 3 cheats adult card games the way of the master kirk cameron bikini bathing suit ask jeeves co yo moma jokes victoriasecret johnny depp pirates pictures xxx free passwords planning a baby shower jeopardy video games white sand beach chilis restaurant girl lover mazda 3 body parts martial arts instruction video pittsburgh pirates hat medical receptionist education pregnant milf malaria drug treatment swimming strokes video eazy e music videos diode laser modules butchart gardens pax movie jewel staite photo janice orange uk phone number paki url sitemap tommy tutone ringtone index nutrisystem adipex reading music girls humping furniture stabbin webmap cat hentai nick stokes moexipril drug adipex p pharmacy online sepultura mp3 webmap zap and roger music map of us states and cities vasoactive drugs american idol audition videos season 4 vital signs 2006 rompl edi healthclaims network licensee 26 34 34b sexymodels
éÂÂç¯ÂæÂÂ章說æ 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 æÂÂ件ãÂÂ