AJAX (Òì²½ JavaScript ºÍ XML) ÊǸöвúÉúµÄÊõÓï,רΪÃèÊöJavaScriptµÄÁ½ÏîÇ¿´óÐÔÄÜ.ÕâÁ½ÏîÐÔ
ÄÜÔÚ¶àÄêÀ´Ò»Ö±±»ÍøÂ翪·¢ÕßËùºöÂÔ,Ö±µ½×î½üGmail, Google suggestºÍgoogle MapsµÄºá¿Õ³öÊÀ²ÅʹÈË
ÃÇ¿ªÊ¼Òâʶµ½ÆäÖØÒªÐÔ.
ÕâÁ½Ïî±»ºöÊÓµÄÐÔÄÜÊÇ:
* ÎÞÐèÖØÐÂ×°ÔØÕû¸öÒ³Ãæ±ãÄÜÏò·þÎñÆ÷·¢ËÍÇëÇó.
* ¶ÔXMLÎĵµµÄ½âÎöºÍ´¦Àí£®
²½Öè 1 – "Çë!" --- ÈçºÎ·¢ËÍÒ»¸öHTTPÇëÇó
ΪÁËÓÃJavaScriptÏò·þÎñÆ÷·¢ËÍÒ»¸öHTTPÇëÇó, ÐèÒªÒ»¸ö¾ß±¸ÕâÖÖ¹¦ÄܵÄÀàʵÀý.
ÕâÑùµÄÀàÊ×ÏÈÓÉ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");
}
(ÉÏÀý¶Ô´úÂë×öÁËÒ»¶¨¼ò»¯,ÕâÊÇΪÁ˽âÊÍÈçºÎ´´½¨XMLHTTPÀàʵÀý.
ʵ¼ÊµÄ´úÂëʵÀý¿É²ÎÔı¾Æª²½Öè3.)
Èç¹û·þÎñÆ÷µÄÏìӦûÓÐXML mime-type header,ijЩMozillaä¯ÀÀÆ÷¿ÉÄÜÎÞ·¨Õý³£¹¤×÷. ΪÁ˽â¾öÕâ¸ö
ÎÊÌâ, Èç¹û·þÎñÆ÷ÏìÓ¦µÄheader²»ÊÇtext/xml,¿ÉÒÔµ÷ÓÃÆäËü·½·¨Ð޸ĸÃheader.
http_request = new XMLHttpRequest();
http_request.overrideMimeType('text/xml');
½ÓÏÂÀ´Òª¾ö¶¨µ±ÊÕµ½·þÎñÆ÷µÄÏìÓ¦ºó,ÐèÒª×öʲô.ÕâÐèÒª¸æËßHTTPÇëÇó¶ÔÏóÓÃÄÄÒ»¸öJavaScriptº¯Êý
´¦ÀíÕâ¸öÏìÓ¦.¿ÉÒÔ½«¶ÔÏóµÄonreadystatechangeÊôÐÔÉèÖÃΪҪʹÓõÄJavaScriptµÄº¯ÊýÃû,ÈçÏÂËùʾ:
http_request.onreadystatechange = nameOfTheFunction;
×¢Òâ:ÔÚº¯ÊýÃûºóûÓÐÀ¨ºÅ,Ò²ÎÞÐè´«µÝ²ÎÊý.ÁíÍ⻹ÓÐÒ»ÖÖ·½·¨,¿ÉÒÔÔÚìéÒ³(fly)Öж¨Ò庯Êý¼°Æä¶Ô
ÏìÓ¦Òª²ÉÈ¡µÄÐÐΪ,ÈçÏÂËùʾ:
http_request.onreadystatechange = function(){
// do the thing
};
ÔÚ¶¨ÒåÁËÈçºÎ´¦ÀíÏìÓ¦ºó,¾ÍÒª·¢ËÍÇëÇóÁË.¿ÉÒÔµ÷ÓÃHTTPÇëÇóÀàµÄopen()ºÍsend()·½·¨, ÈçÏÂËùʾ:
http_request.open('GET', 'http://www.example.org/some.file', true);
http_request.send(null);
* open()µÄµÚÒ»¸ö²ÎÊýÊÇHTTPÇëÇó·½Ê½ – GET, POST, HEAD »òÈκηþÎñÆ÷ËùÖ§³ÖµÄÄúÏëµ÷Óõķ½Ê½
. °´ÕÕHTTP¹æ·¶,¸Ã²ÎÊýÒª´óд;·ñÔò,ijЩä¯ÀÀÆ÷(ÈçFirefox)¿ÉÄÜÎÞ·¨´¦ÀíÇëÇó.ÓйØHTTPÇëÇó·½·¨µÄÏê
ϸÐÅÏ¢¿É²Î¿¼