AJAX 上手篇
出自 MozTW Wiki
rock hudson jane wyman movie queensland learners licence test karas xxx free video record flash movies new movie releases to video movie coloring pages james newman videogames jeux video.com panasonic 3ccd video cameras susan selles movie aguilera photo shoot video extreme ops movie cast second x file movie indianmoviesonline prodigy breathe music video
éÃÂÃÂçïÃÂæÃÂÃÂçëàèêêæÃÂà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 æÃÂÃÂäûöãÃÂÃÂ