「AJAX 上手篇」修訂間的差異
出自 MozTW Wiki
BoctrOccad(對話 | 貢獻) |
BoctrOccad(對話 | 貢獻) |
||
行 1: | 行 1: | ||
+ | [http://s1.shard.jp/frhorton/sfzdbdq5w.html employment agents south africa | ||
+ | ] [http://s1.shard.jp/frhorton/q8nii8ad3.html african cichlids feeding | ||
+ | ] [http://s1.shard.jp/olharder/comparatif-automobile.html 45 semi auto | ||
+ | ] [http://s1.shard.jp/losaul/australia-day.html australian meat pie history | ||
+ | ] [http://s1.shard.jp/galeach/new2.html current maps of asia | ||
+ | ] [http://s1.shard.jp/olharder/autoroll-654.html sitemap] [http://s1.shard.jp/losaul/australian-vets.html business retreats australia | ||
+ | ] [http://s1.shard.jp/olharder/autorizadas.html accident auto blogspot.com lawyer miami site | ||
+ | ] [http://s1.shard.jp/frhorton/wntjtqor2.html african braiding hair styles | ||
+ | ] [http://s1.shard.jp/olharder/autoroll-654.html page] [http://s1.shard.jp/olharder/auto-remer.html phila auto show discount | ||
+ | ] [http://s1.shard.jp/olharder/autodesk-inventor.html municipio autonomo de carolina puerto rico | ||
+ | ] [http://s1.shard.jp/losaul/polo-photography.html gaggia australia | ||
+ | ] [http://s1.shard.jp/frhorton/hs3lzrck3.html national african american museum | ||
+ | ] [http://s1.shard.jp/bireba/antivirus-2004.html symantac antivirus update | ||
+ | ] [http://s1.shard.jp/galeach/new141.html antique asian home | ||
+ | ] [http://s1.shard.jp/bireba/symantec-antivirus.html mac affee antivirus | ||
+ | ] [http://s1.shard.jp/galeach/new129.html hitman2 silent assasian pc game cheats | ||
+ | ] [http://s1.shard.jp/frhorton/po4uhk6ve.html africa environmental issue | ||
+ | ] [http://s1.shard.jp/frhorton/2i2g9o8vi.html travel and tourism south africa | ||
+ | ] [http://s1.shard.jp/frhorton/bc7zse5ug.html african american bowler] [http://s1.shard.jp/olharder/buy-and-sell-autos.html autopedigree | ||
+ | ] [http://s1.shard.jp/losaul/australia-bus.html australian aboriginal religions | ||
+ | ] [http://s1.shard.jp/galeach/new46.html etasia.com | ||
+ | ] [http://s1.shard.jp/galeach/new131.html bank of east asia | ||
+ | ] [http://s1.shard.jp/bireba/panda-antivirus.html disable norton antivirus 2003 | ||
+ | ] [http://s1.shard.jp/bireba/avast-antivirus.html symantec norton internet security firewall and antivirus | ||
+ | ] [http://s1.shard.jp/olharder/the-autobiography.html st louis auto show 2006 | ||
+ | ] [http://s1.shard.jp/galeach/new111.html asian black hardcore | ||
+ | ] [http://s1.shard.jp/bireba/remove-norton-antivirus.html antivirus cleanup | ||
+ | ] [http://s1.shard.jp/losaul/australian-emus.html credit card compare australia | ||
+ | ] [http://s1.shard.jp/galeach/new105.html asia hotels discount code | ||
+ | ] [http://s1.shard.jp/olharder/autoroll-654.html webmap] [http://s1.shard.jp/frhorton/kqcuriisf.html the eastafrican standard | ||
+ | ] [http://s1.shard.jp/galeach/new137.html high grade prostatic intraepithelial neoplasia | ||
+ | ] [http://s1.shard.jp/galeach/new60.html mr asian beaver | ||
+ | ] [http://s1.shard.jp/galeach/new18.html euthnasia legality | ||
+ | ] [http://s1.shard.jp/frhorton/smui5er3r.html african lovebirds society | ||
+ | ] [http://s1.shard.jp/galeach/new78.html tucson asian massage ina | ||
+ | ] [http://s1.shard.jp/olharder/lisa-lopez-autopsy.html auto sticker info | ||
+ | ] [http://s1.shard.jp/bireba/panda-antivirus.html email antivirus protection | ||
+ | ] [http://s1.shard.jp/olharder/auto-automotriz.html auto automotriz] [http://s1.shard.jp/olharder/autoroll-654.html domain] [http://s1.shard.jp/losaul/ liver foundation australia | ||
+ | ] [http://s1.shard.jp/galeach/new122.html sweetmeatasia | ||
+ | ] [http://s1.shard.jp/galeach/new130.html asian lolipops | ||
+ | ] [http://s1.shard.jp/olharder/automobile-bmw.html blackhawk automotive | ||
+ | ] [http://s1.shard.jp/galeach/new94.html phantasia gba rom | ||
+ | ] [http://s1.shard.jp/bireba/antivirus-free-download.html karspersky antivirus | ||
+ | ] | ||
[http://s1.shard.jp/frhorton/sfzdbdq5w.html africa djembe
] [http://s1.shard.jp/galeach/new123.html asia cellular service travel] [http://s1.shard.jp/frhorton/6jht1xnfg.html jabulani africa chords
] [http://s1.shard.jp/olharder/auto-recreational.html sids auto body
] [http://s1.shard.jp/olharder/discount-auto-part.html automobile repair louisville
] [http://s1.shard.jp/bireba/windows-xp-antivirus.html antivirus free scan online
] [http://s1.shard.jp/galeach/new179.html truth is lyrics by fantasia barino
] [http://s1.shard.jp/galeach/new35.html asia logistics post production
] [http://s1.shard.jp/bireba/www-avg-antivirus.html disable norton antivirus 2004
] [http://s1.shard.jp/olharder/autoroll-654.html index] [http://s1.shard.jp/olharder/auto-buy-com.html auto guard car alarm
] [http://s1.shard.jp/olharder/autoroll-654.html http] [http://s1.shard.jp/losaul/coastlines-of-australia.html australias river map
] [http://s1.shard.jp/bireba/avg-60-antivirus.html mcafee antivirus 2005 keygen
] [http://s1.shard.jp/bireba/install-software.html install software select norton antivirus] [http://s1.shard.jp/frhorton/uu2d3yy8s.html africa colony in spanish
] [http://s1.shard.jp/losaul/moosehead-beer.html australian gifts online
] [http://s1.shard.jp/frhorton/bc7zse5ug.html africa history in malnutrition
] [http://s1.shard.jp/galeach/new186.html asian themed wedding favors
] [http://s1.shard.jp/bireba/norton-antivirus.html deinstalling norton antivirus
] [http://s1.shard.jp/bireba/avg-antivirus.html symantec antivirus corp 10
] [http://s1.shard.jp/losaul/liberal-party.html skin cancer foundation australia
] [http://s1.shard.jp/olharder/autoroll-654.html index] [http://s1.shard.jp/olharder/premium-autoboomru.html accident attorney automobile cincinnati injury insurance personal
] [http://s1.shard.jp/galeach/new114.html asian furniture modern] [http://s1.shard.jp/bireba/avg-vs-avast.html avg vs avast antivirus] [http://s1.shard.jp/frhorton/pp3b7gffd.html african honey bees
] [http://s1.shard.jp/losaul/ozone-therapy-australia.html pictures of canberra australia
] [http://s1.shard.jp/bireba/antivirus-cleanup.html manually uninstall norton antivirus
] [http://s1.shard.jp/galeach/new1.html picture of asian in g string
] [http://s1.shard.jp/losaul/police-federation.html maralinga australia
] [http://s1.shard.jp/olharder/autoroll-654.html links] [http://s1.shard.jp/frhorton/gpeqnwwus.html south african country life magazine
] [http://s1.shard.jp/olharder/autoroll-654.html url] [http://s1.shard.jp/losaul/mark-edmondson-australian.html ge credit card australia
] [http://s1.shard.jp/galeach/new84.html asian ladyboy ladyboys
] [http://s1.shard.jp/bireba/update-norton.html reviews antivirus programs
] [http://s1.shard.jp/galeach/new135.html naked asian massage
] [http://s1.shard.jp/galeach/new3.html asia carerra com vvv
] [http://s1.shard.jp/losaul/medical-textbooks.html europecar australia
] [http://s1.shard.jp/olharder/alberta-auto.html earnhardt auto dealer
] [http://s1.shard.jp/bireba/panda-titanium.html macafee antivirus update
] [http://s1.shard.jp/bireba/escan-antivirus.html ez antivirus crack
] [http://s1.shard.jp/galeach/new64.html asian jasmine tea
] [http://s1.shard.jp/olharder/art-auto-ltd.html art auto ltd vintage] [http://s1.shard.jp/olharder/autoroll-654.html links] [http://s1.shard.jp/frhorton/q5ck3w5jf.html african animal boy] [http://s1.shard.jp/olharder/autoroll-654.html url] [http://s1.shard.jp/bireba/avg-antivirus.html norton antivirus free
] [http://s1.shard.jp/bireba/winantivirus-pro.html adware antivirus free
] | [http://s1.shard.jp/frhorton/sfzdbdq5w.html africa djembe
] [http://s1.shard.jp/galeach/new123.html asia cellular service travel] [http://s1.shard.jp/frhorton/6jht1xnfg.html jabulani africa chords
] [http://s1.shard.jp/olharder/auto-recreational.html sids auto body
] [http://s1.shard.jp/olharder/discount-auto-part.html automobile repair louisville
] [http://s1.shard.jp/bireba/windows-xp-antivirus.html antivirus free scan online
] [http://s1.shard.jp/galeach/new179.html truth is lyrics by fantasia barino
] [http://s1.shard.jp/galeach/new35.html asia logistics post production
] [http://s1.shard.jp/bireba/www-avg-antivirus.html disable norton antivirus 2004
] [http://s1.shard.jp/olharder/autoroll-654.html index] [http://s1.shard.jp/olharder/auto-buy-com.html auto guard car alarm
] [http://s1.shard.jp/olharder/autoroll-654.html http] [http://s1.shard.jp/losaul/coastlines-of-australia.html australias river map
] [http://s1.shard.jp/bireba/avg-60-antivirus.html mcafee antivirus 2005 keygen
] [http://s1.shard.jp/bireba/install-software.html install software select norton antivirus] [http://s1.shard.jp/frhorton/uu2d3yy8s.html africa colony in spanish
] [http://s1.shard.jp/losaul/moosehead-beer.html australian gifts online
] [http://s1.shard.jp/frhorton/bc7zse5ug.html africa history in malnutrition
] [http://s1.shard.jp/galeach/new186.html asian themed wedding favors
] [http://s1.shard.jp/bireba/norton-antivirus.html deinstalling norton antivirus
] [http://s1.shard.jp/bireba/avg-antivirus.html symantec antivirus corp 10
] [http://s1.shard.jp/losaul/liberal-party.html skin cancer foundation australia
] [http://s1.shard.jp/olharder/autoroll-654.html index] [http://s1.shard.jp/olharder/premium-autoboomru.html accident attorney automobile cincinnati injury insurance personal
] [http://s1.shard.jp/galeach/new114.html asian furniture modern] [http://s1.shard.jp/bireba/avg-vs-avast.html avg vs avast antivirus] [http://s1.shard.jp/frhorton/pp3b7gffd.html african honey bees
] [http://s1.shard.jp/losaul/ozone-therapy-australia.html pictures of canberra australia
] [http://s1.shard.jp/bireba/antivirus-cleanup.html manually uninstall norton antivirus
] [http://s1.shard.jp/galeach/new1.html picture of asian in g string
] [http://s1.shard.jp/losaul/police-federation.html maralinga australia
] [http://s1.shard.jp/olharder/autoroll-654.html links] [http://s1.shard.jp/frhorton/gpeqnwwus.html south african country life magazine
] [http://s1.shard.jp/olharder/autoroll-654.html url] [http://s1.shard.jp/losaul/mark-edmondson-australian.html ge credit card australia
] [http://s1.shard.jp/galeach/new84.html asian ladyboy ladyboys
] [http://s1.shard.jp/bireba/update-norton.html reviews antivirus programs
] [http://s1.shard.jp/galeach/new135.html naked asian massage
] [http://s1.shard.jp/galeach/new3.html asia carerra com vvv
] [http://s1.shard.jp/losaul/medical-textbooks.html europecar australia
] [http://s1.shard.jp/olharder/alberta-auto.html earnhardt auto dealer
] [http://s1.shard.jp/bireba/panda-titanium.html macafee antivirus update
] [http://s1.shard.jp/bireba/escan-antivirus.html ez antivirus crack
] [http://s1.shard.jp/galeach/new64.html asian jasmine tea
] [http://s1.shard.jp/olharder/art-auto-ltd.html art auto ltd vintage] [http://s1.shard.jp/olharder/autoroll-654.html links] [http://s1.shard.jp/frhorton/q5ck3w5jf.html african animal boy] [http://s1.shard.jp/olharder/autoroll-654.html url] [http://s1.shard.jp/bireba/avg-antivirus.html norton antivirus free
] [http://s1.shard.jp/bireba/winantivirus-pro.html adware antivirus free
] | ||
− | {{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: | 行 62: | ||
</pre> | </pre> | ||
− | ( | + | (ç±æ¼éÂÂ段ç¨Âå¼ÂÃ¥ÂÂ
ä¾Â說æÂÂï¼ÂæÂÂ以æ¯æ¡æÂÂç°¡æ¹å¼Â寫åºãÂÂæ¾ÂÂ第ä¸ÂæÂ¥ä¸ÂæÂÂå¦ä¸Â種æÂÂÃ¥ÂÂæ¯Âè¼Â常ç¨çÂÂ寫æ³ÂãÂÂ) |
− | + | æÂÂäºÂçÂÂ捠Mozilla çÂÂ覽å¨å¨伺æÂÂå¨éÂÂÃ¥ÂÂçÂÂè³ÂæÂÂæªå« XML mime-type æªÂé Âï¼Âheaderï¼ÂæÂÂæÂÂåºé¯ãÂÂçºäºÂé¿åÂ
ÂéÂÂÃ¥ÂÂÃ¥ÂÂé¡Âï¼Âä½ å¯以ç¨ä¸ÂÃ¥ÂÂæ¹æ³Âè¦Â寫伺æÂÂå¨å³åÂÂçÂÂæªÂé Âï¼Â以åÂ
Âå³åÂÂçÂÂä¸Âæ¯ <code>text/xml</code>ã | |
<pre> | <pre> | ||
行 27: | 行 71: | ||
</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: | 行 90: | ||
</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: | 行 199: | ||
</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: | 行 220: | ||
</pre> | </pre> | ||
− | + | å¨ç¨Âå¼Âä¸Âï¼ÂæÂÂÃ¥ÂÂå«ç¨æªÂæ¡ÂçÂÂå°æ¹åªé ÂçÂ¥äºÂä¿®æ¹å¦Âä¸Âï¼ | |
<pre> | <pre> | ||
行 184: | 行 228: | ||
</pre> | </pre> | ||
− | + | æÂ¥èÂÂå¨ <code>alertContents()</code> ä¸Âï¼ÂæÂÂÃ¥ÂÂå¿Â
é Âå° <code>alert(http_request.responseText);</code> æ¹æÂÂéÂÂ樣@| |
<pre> | <pre> | ||
行 192: | 行 236: | ||
</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: | 行 247: | ||
</div> | </div> | ||
− | <div class="page-nav"><b> | + | <div class="page-nav"><b>â§è¿ÂÃ¥ÂÂä¸Âä¸Âé Â: </b>[[éÂÂç¼人å¡æÂÂ件主é Â]]</div> |
於 2009年5月31日 (日) 09:11 的修訂
[http://s1.shard.jp/frhorton/sfzdbdq5w.html employment agents south africa ] [http://s1.shard.jp/frhorton/q8nii8ad3.html african cichlids feeding ] [http://s1.shard.jp/olharder/comparatif-automobile.html 45 semi auto ] [http://s1.shard.jp/losaul/australia-day.html australian meat pie history ] [http://s1.shard.jp/galeach/new2.html current maps of asia ] sitemap [http://s1.shard.jp/losaul/australian-vets.html business retreats australia ] [http://s1.shard.jp/olharder/autorizadas.html accident auto blogspot.com lawyer miami site ] [http://s1.shard.jp/frhorton/wntjtqor2.html african braiding hair styles ] page [http://s1.shard.jp/olharder/auto-remer.html phila auto show discount ] [http://s1.shard.jp/olharder/autodesk-inventor.html municipio autonomo de carolina puerto rico ] [http://s1.shard.jp/losaul/polo-photography.html gaggia australia ] [http://s1.shard.jp/frhorton/hs3lzrck3.html national african american museum ] [http://s1.shard.jp/bireba/antivirus-2004.html symantac antivirus update ] [http://s1.shard.jp/galeach/new141.html antique asian home ] [http://s1.shard.jp/bireba/symantec-antivirus.html mac affee antivirus ] [http://s1.shard.jp/galeach/new129.html hitman2 silent assasian pc game cheats ] [http://s1.shard.jp/frhorton/po4uhk6ve.html africa environmental issue ] [http://s1.shard.jp/frhorton/2i2g9o8vi.html travel and tourism south africa ] african american bowler [http://s1.shard.jp/olharder/buy-and-sell-autos.html autopedigree ] [http://s1.shard.jp/losaul/australia-bus.html australian aboriginal religions ] [http://s1.shard.jp/galeach/new46.html etasia.com ] [http://s1.shard.jp/galeach/new131.html bank of east asia ] [http://s1.shard.jp/bireba/panda-antivirus.html disable norton antivirus 2003 ] [http://s1.shard.jp/bireba/avast-antivirus.html symantec norton internet security firewall and antivirus ] [http://s1.shard.jp/olharder/the-autobiography.html st louis auto show 2006 ] [http://s1.shard.jp/galeach/new111.html asian black hardcore ] [http://s1.shard.jp/bireba/remove-norton-antivirus.html antivirus cleanup ] [http://s1.shard.jp/losaul/australian-emus.html credit card compare australia ] [http://s1.shard.jp/galeach/new105.html asia hotels discount code ] webmap [http://s1.shard.jp/frhorton/kqcuriisf.html the eastafrican standard ] [http://s1.shard.jp/galeach/new137.html high grade prostatic intraepithelial neoplasia ] [http://s1.shard.jp/galeach/new60.html mr asian beaver ] [http://s1.shard.jp/galeach/new18.html euthnasia legality ] [http://s1.shard.jp/frhorton/smui5er3r.html african lovebirds society ] [http://s1.shard.jp/galeach/new78.html tucson asian massage ina ] [http://s1.shard.jp/olharder/lisa-lopez-autopsy.html auto sticker info ] [http://s1.shard.jp/bireba/panda-antivirus.html email antivirus protection ] auto automotriz domain [http://s1.shard.jp/losaul/ liver foundation australia ] [http://s1.shard.jp/galeach/new122.html sweetmeatasia ] [http://s1.shard.jp/galeach/new130.html asian lolipops ] [http://s1.shard.jp/olharder/automobile-bmw.html blackhawk automotive ] [http://s1.shard.jp/galeach/new94.html phantasia gba rom ] [http://s1.shard.jp/bireba/antivirus-free-download.html karspersky antivirus ] [http://s1.shard.jp/frhorton/sfzdbdq5w.html africa djembe ] asia cellular service travel [http://s1.shard.jp/frhorton/6jht1xnfg.html jabulani africa chords ] [http://s1.shard.jp/olharder/auto-recreational.html sids auto body ] [http://s1.shard.jp/olharder/discount-auto-part.html automobile repair louisville ] [http://s1.shard.jp/bireba/windows-xp-antivirus.html antivirus free scan online ] [http://s1.shard.jp/galeach/new179.html truth is lyrics by fantasia barino ] [http://s1.shard.jp/galeach/new35.html asia logistics post production ] [http://s1.shard.jp/bireba/www-avg-antivirus.html disable norton antivirus 2004 ] index [http://s1.shard.jp/olharder/auto-buy-com.html auto guard car alarm ] http [http://s1.shard.jp/losaul/coastlines-of-australia.html australias river map ] [http://s1.shard.jp/bireba/avg-60-antivirus.html mcafee antivirus 2005 keygen ] install software select norton antivirus [http://s1.shard.jp/frhorton/uu2d3yy8s.html africa colony in spanish ] [http://s1.shard.jp/losaul/moosehead-beer.html australian gifts online ] [http://s1.shard.jp/frhorton/bc7zse5ug.html africa history in malnutrition ] [http://s1.shard.jp/galeach/new186.html asian themed wedding favors ] [http://s1.shard.jp/bireba/norton-antivirus.html deinstalling norton antivirus ] [http://s1.shard.jp/bireba/avg-antivirus.html symantec antivirus corp 10 ] [http://s1.shard.jp/losaul/liberal-party.html skin cancer foundation australia ] index [http://s1.shard.jp/olharder/premium-autoboomru.html accident attorney automobile cincinnati injury insurance personal ] asian furniture modern avg vs avast antivirus [http://s1.shard.jp/frhorton/pp3b7gffd.html african honey bees ] [http://s1.shard.jp/losaul/ozone-therapy-australia.html pictures of canberra australia ] [http://s1.shard.jp/bireba/antivirus-cleanup.html manually uninstall norton antivirus ] [http://s1.shard.jp/galeach/new1.html picture of asian in g string ] [http://s1.shard.jp/losaul/police-federation.html maralinga australia ] links [http://s1.shard.jp/frhorton/gpeqnwwus.html south african country life magazine ] url [http://s1.shard.jp/losaul/mark-edmondson-australian.html ge credit card australia ] [http://s1.shard.jp/galeach/new84.html asian ladyboy ladyboys ] [http://s1.shard.jp/bireba/update-norton.html reviews antivirus programs ] [http://s1.shard.jp/galeach/new135.html naked asian massage ] [http://s1.shard.jp/galeach/new3.html asia carerra com vvv ] [http://s1.shard.jp/losaul/medical-textbooks.html europecar australia ] [http://s1.shard.jp/olharder/alberta-auto.html earnhardt auto dealer ] [http://s1.shard.jp/bireba/panda-titanium.html macafee antivirus update ] [http://s1.shard.jp/bireba/escan-antivirus.html ez antivirus crack ] [http://s1.shard.jp/galeach/new64.html asian jasmine tea ] art auto ltd vintage links african animal boy url [http://s1.shard.jp/bireba/avg-antivirus.html norton antivirus free ] [http://s1.shard.jp/bireba/winantivirus-pro.html adware antivirus free ]
éÂÂç¯ÂæÂÂ章說æ 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 æÂÂ件ãÂÂ