AJAX 上手篇
出自 MozTW Wiki
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 æÂÂ件ãÂÂ