ÎÒҪ˵µÄÄÚÈݶ¼ÊǷdz£»ù´¡µÄÄÚÈÝ£¬¸ßÊÖ¾ÍÃâ¿´ÁË£¬Èç¹û¿´ÁË»¶Ó¸øµãÒâ¼û°¡¡£ÐÂÊÖ»òÕ߶ԵͲ㻹²»ÊǺÜÁ˽âµÄÈË¿ÉÒÔ¿´¿´£¬°ïÖúÀí½âÓë¼ÇÒä¡£ XMLHttpRequest ¶ÔÏóÊÇAJAX¹¦ÄܵĺËÐÄ£¬Òª¿ª·¢AJAX³ÌÐò±ØÐë´ÓÁ˽âXMLHttpRequest ¶ÔÏó¿ªÊ¼¡£ Á˽âXMLHttpRequest ¶ÔÏó¾ÍÏÈ´Ó´´½¨XMLHttpRequest ¶ÔÏó¿ªÊ¼£¬ÔÚ²»Í¬µÄä¯ÀÀÆ÷Öд´½¨XMLHttpRequest ¶ÔÏóʹÓò»Í¬µÄ·½·¨£º ÏÈ¿´¿´IE´´½¨XMLHttpRequest ¶ÔÏóµÄ·½·¨£¨·½·¨1£©£º var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); //ʹÓýÏа汾µÄ IE ´´½¨ IE ¼æÈݵĶÔÏó£¨Msxml2.XMLHTTP£©
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //ʹÓýÏÀÏ°æ±¾µÄ IE ´´½¨ IE ¼æÈݵĶÔÏó£¨Microsoft.XMLHTTP£© ¶ø Mozilla¡¢Opera¡¢Safari ºÍ´ó²¿·Ö·ÇIEµÄä¯ÀÀÆ÷¶¼Ê¹ÓÃÏÂÃæÕâÖÖ·½·¨£¨·½·¨2£©´´½¨XMLHttpRequest ¶ÔÏó£º var xmlhttp = new XMLHttpRequest();
ʵ¼ÊÉÏInternet Explorer ʹÓÃÁËÒ»¸öÃûΪ XMLHttp µÄ¶ÔÏ󣬶ø²»ÊÇ XMLHttpRequest ¶ÔÏ󣬶ø Mozilla¡¢Opera¡¢Safari ºÍ ´ó²¿·Ö·Ç Microsoft ä¯ÀÀÆ÷¶¼Ê¹ÓõÄÊǺóÕߣ¨ÏÂÎÄͳ³Æ XMLHttpRequest ¶ÔÏ󣩡£IE7¿ªÊ¼Ò²¿ªÊ¼Ê¹ÓÃXMLHttpRequest ¶ÔÏóÁË¡£ ÔÚ´´½¨ XMLHttpRequest ¶ÔÏóµÄʱºòÈç¹û²»Í¬µÄä¯ÀÀÆ÷ʹÓÃÁ˲»ÕýÈ·µÄ·½·¨ä¯ÀÀÆ÷¶¼½«»á±¨´í£¬²¢ÇÒÎÞ·¨Ê¹ÓøöÔÏó¡£ËùÒÔÎÒÃÇÐèÒªÒ»ÖÖ¿ÉÒÔ¼æÈݲ»Í¬ä¯ÀÀÆ÷µÄ´´½¨XMLHttpRequest ¶ÔÏóµÄ·½·¨£º ´´½¨¼æÈݶàä¯ÀÀÆ÷µÄ XMLHttpRequest ¶ÔÏó·½·¨
var xmlhttp = false; //´´½¨Ò»¸öбäÁ¿ request ²¢¸³Öµ false¡£Ê¹Óà false ×÷ΪÅжÏÌõ¼þ£¬Ëü±íʾ»¹Ã»Óд´½¨ XMLHttpRequest ¶ÔÏó¡£ function CreateXMLHttp(){ try{ xmlhttp = new XMLHttpRequest(); //³¢ÊÔ´´½¨ XMLHttpRequest ¶ÔÏ󣬳ý IE ÍâµÄä¯ÀÀÆ÷¶¼Ö§³ÖÕâ¸ö·½·¨¡£ } catch (e){ try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); //ʹÓýÏа汾µÄ IE ´´½¨ IE ¼æÈݵĶÔÏó£¨Msxml2.XMLHTTP£© } catch (e){ try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //ʹÓýÏÀÏ°æ±¾µÄ IE ´´½¨ IE ¼æÈݵĶÔÏó£¨Microsoft.XMLHTTP£©¡£ } catch (failed){ xmlhttp = false; //Èç¹ûʧ°ÜÔò±£Ö¤ request µÄÖµÈÔȻΪ false¡£ } } } return xmlhttp; } ÅжÏÊÇ·ñ´´½¨³É¹¦¾ÍºÜ¼òµ¥ÁË if (!xmlhttp){
//´´½¨XMLHttpRequest ¶ÔÏóʧ°Ü! } else{ //´´½¨³É¹¦! } ´´½¨ºÃÁËXMLHttpRequest ¶ÔÏóÎÒÃÇÔÙÀ´¿´¿´Õâ¸ö¶ÔÏóµÄ·½·¨¡¢ÊôÐÔÒÔ¼°×îÖØÒªµÄonreadystatechangeʼþ¾ä±ú°É¡£ ·½·¨£º
ÊôÐÔ£º
ÏÂÃæ´Ó·¢ËÍÇëÇó²¢´¦ÀíÇëÇó½á¹ûµÄ¹ý³ÌÀ´Àí½âÒ»ÏÂXMLHttpRequest ¶ÔÏó°É¡£ ·¢ËÍÇëÇó֮ǰ×ÔÈ»¾ÍÊÇÉú³ÉÒ»¸öXMLHttpRequest ¶ÔÏ󣬴úÂëÉÏÃæÓÐÁ˾Ͳ»¶àдÁË¡£ Éú³ÉÒ»¸öXMLHttpRequest ¶ÔÏó var xmlhttp = CreateXMLHttp();
´´½¨ºÃXMLHttpRequest ¶ÔÏóÁË£¬ÄÇÎÒÃÇÒªËÍÇëÇóµ½ÄĸöÍøÕ¾ÄØ£¬¾ÍÑ¡Ôñ²©¿ÍÔ°Ê×Ò³µÄRSS°É¡£ÄÇÔõôÉèÖÃÎÒÒªÇëÇóµÄÍøÕ¾µØÖ·ÄØ£¬Ê¹ÓÃopen()·½·¨¡£ xmlHttp.open("get","http://www.cnblogs.com",true);
get²ÎÊý±íʾÓÃget·½·¨£¬µÚ¶þ¸ö×ÔÈ»¾ÍÊÇÄ¿±êµØÖ·£¬²©¿ÍÔ°Ê×Ò³£¬µÚÈý¸ö¾ÍÊDZíʾÊÇ·ñÒì²½ÁË£¬ÎÒÃǵ±È»Ê¹ÓÃtrueÁË¡£¾ßÌåµÄ²ÎÊý˵Ã÷»¹¶¼¿ÉÒÔµ½http://www.w3school.com.cnÉÏÃæ¿´ÁË¡£
xmlhttp.send(null);
¾Í¿ÉÒÔÁË¡£ºÃÁË£¬·¢ËÍÁË£¬ÄÇÔõô´¦Àí·µ»ØµÄ½á¹ûÄØ£¬Õâ¸öʱºò¾ÍÓõ½XMLHttpRequest ¶ÔÏó×îÖØÒªµÄ¶«Î÷ÁË£¬ÄǾÍÊÇonreadystatechangeʼþ¾ä±ú¡£Ê²Ã´Òâ˼ÄØ£¬ÄǾÍÐèҪ˵Ã÷Ò»ÏÂXMLHttpRequest ¶ÔÏóµÄreadyStateÁË£¬readyStateÓÐ5ÖÖ״̬£¬·Ö±ðÓÃÊý×ÖµÄ 0 µ½ 4 À´±íʾ¡£ ״̬ Ãû³Æ ÃèÊö µ«ÊÇÐèҪעÒâµÄÊÇ£¬onreadystatechangeʼþ¾ä±ú²»Í¬µÄä¯ÀÀÆ÷ÄÜ´¦ÀíµÄ״̬²¢²»Ò»Ö£¬IEºÍFireFoxÄÜ´¦Àí1µ½4£¬¶øSafariÄÜ´¦Àí2µ½4µÄ״̬£¬Opera ÄÜ´¦Àí3¡¢4Á½ÖÐ״̬¡£0µÄ״̬»ù±¾Ã»Ê²Ã´Óã¬ÒòΪ´´½¨ÁËXMLHttpRequest ¶ÔÏóºó¶¼»áÂíÉϵ÷ÓÃopen() ·½·¨£¬Õâʱºò״̬¾Í±ä³É1ÁË£¬µ±È»³ý·ÇÄãÒªÅж϶ÔÏóÊÇ·ñÒѾ±» abort() È¡Ïû£¬¿ÉÊÇÕâÑùµÄÇé¿öÒÀÈ»ºÜÉÙ¡£´ó²¿·ÖÇé¿öÏÂÅжÏÊDz»ÊÇ4£¨ÒѾ½ÓÊÜÍê³É£©Õâ¸ö״̬¾Í¹»ÁË¡£ xmlhttp.onReadyStateChange = function (){
//´¦Àí״̬±ä»¯µÄ´úÂë } //»òÕß xmlhttp.onReadyStateChange = getResult; function getResult(){ ///´¦Àí״̬±ä»¯µÄ´úÂë } //˳±ã˵һÏ£¬¾ä±úµÄÃû³Æ±È½Ï³¤£¬¿ÉÒÔÕâÑù¼ÇÒä on ReadyState Change ±íʾÔÚ¶Áȡ״̬¸Ä±äʱ ÇëÇó·¢ËÍÁË£¬Ò²Ö¸¶¨´¦Àí·½·¨ÁË£¬Ôõô»ñÈ¡·µ»ØµÄÄÚÈÝÄØ£¬ÓÐresponseTextºÍresponseXMLÁ½¸öÊôÐԿɹ©Ê¹Óã¬responseXMLÊÇ·µ»Ø¶ÔÏó£¬ÐèÒªÔÙ½âÎö£¬ºóÃæÔÙ˵£¬ÕâÀïÏÈÓÃresponseText£¬¿´¿´·µ»Øʲô¡£ alert(xmlhttp.responseText); //¿´¿´ÊDz»ÊÇ·µ»ØÁËÊ×Ò³µÄHTML´úÂë°¡¡£ÊÇÄã¾Í³É¹¦ÁË¡£
Õû¸ö¹ý³ÌÊÇ£º´´½¨XMLHttpRequest ¶ÔÏó -> Ö¸¶¨·¢Ë͵ØÖ·¼°·¢ËÍ·½·¨ -> ·¢ËÍÇëÇó -> Ö¸¶¨´¦Àí·½·¨²¢´¦Àí·µ»Ø½á¹û¡£µ«ÊÇÐèҪעÒ⣬ÎÒÃÇÕý³£µÄ˼·Àí½âÊÇÕâÑùµÄ£¬¿ÉÊÇonreadystatechangeʼþ¾ä±úÖ¸¶¨´¦Àí·½·¨ÐèÒªÔÚ·¢ËÍ֮ǰ¾ÍÖ¸¶¨ºÃ£¬·ñÔòÎÞ·¨´¦Àí״̬±ä»¯Ê¼þ¡£ ËùÒÔÎÒÃÇÓ¦¸Ã°´ÕÕÏÂÃæµÄÁ÷³ÌÀ´¼ÇÒ䣺´´½¨XMLHttpRequest ¶ÔÏó -> Ö¸¶¨·¢Ë͵ØÖ·¼°·¢ËÍ·½·¨ -> Ö¸¶¨×´Ì¬±ä»¯´¦Àí·½·¨ -> ·¢ËÍÇëÇó£¬ÇëÇó·¢Ëͺó״̬±ä»¯Á˾ͻá×Ô¶¯µ÷ÓÃÖ¸¶¨µÄ´¦Àí·½·¨¡£
Íê³ÉµÄ´úÂë
var xmlhttp = false; //´´½¨Ò»¸öбäÁ¿ request ²¢¸³Öµ false¡£Ê¹Óà false ×÷ΪÅжÏÌõ¼þ£¬Ëü±íʾ»¹Ã»Óд´½¨ XMLHttpRequest ¶ÔÏó¡£ function CreateXMLHttp(){ try{ xmlhttp = new XMLHttpRequest(); //³¢ÊÔ´´½¨ XMLHttpRequest ¶ÔÏ󣬳ý IE ÍâµÄä¯ÀÀÆ÷¶¼Ö§³ÖÕâ¸ö·½·¨¡£ } catch (e){ try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); //ʹÓýÏа汾µÄ IE ´´½¨ IE ¼æÈݵĶÔÏó£¨Msxml2.XMLHTTP£© } catch (e){ try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //ʹÓýÏÀÏ°æ±¾µÄ IE ´´½¨ IE ¼æÈݵĶÔÏó£¨Microsoft.XMLHTTP£©¡£ } catch (failed){ xmlhttp = false; //Èç¹ûʧ°ÜÔò±£Ö¤ request µÄÖµÈÔȻΪ false¡£ } } } return xmlhttp; } xmlhttp = CreateXMLHttp(); xmlhttp.open("get","http://www.cnblogs.com",true); xmlhttp.onReadyStateChange = getResult; xmlhttp.send(null); function getResult(){ if(xmlhttp.readyState == 4){ alert(xmlhttp.responseText); } } ¿´ËÆÒ»Çж¼OKÁË£¬¿ÉÊÇÓÐûÓÐÏë¹ý£¬Èç¹ûHTML´úÂëÔÚÍøÂç´«Êä¹ý³ÌÖгö´íÁË£¬»òÕßÎÒÃÇÖ¸¶¨µÄµØַʧЧ»áÔõôÑùÄØ¡£Õâ¸öʱºò¾ÍÐèÒªÓõ½statusÊôÐÔ£¬¼´ÓÉ·þÎñÆ÷·µ»ØµÄ HTTP ״̬´úÂë¡£ xmlhttp.status µÈÓÚ200ʱ±íʾ´«Êä¹ý³ÌÍêÕûûÓдíÎó¡£¾ßÌåµÄHTTP״̬´úÂëʲôÒâ˼¿ÉÒÔµ½W3C×éÖ¯ÍøÕ¾ÉÏ¿´¿´£¬µØÖ·http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1 ¡£ function getResult(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ alert(xmlhttp.responseText); } } ºÃÁË£¬Ò»¸ö±¾À´Í¦¼òµ¥µÄ¶«Î÷£¬±»ÎÒдµÄÕâô¶à£¬ºÃÏóºÜÂÞà¡£²»¹ýÎÒ¾õµÄ±à³Ì¶Ô»ù´¡ÄÚÈݵÄÀí½âºÜÖØÒª£¬ÏÖÔںܶàʱºò¿ª·¢AJAXµÄ³ÌÐò¶¼Ê¹ÓúܶàJSµÄ¿â£¬²»ÐèÒªÖ±½Ó±àдÕâô»ù´¡µÄ´úÂë¡£ÈçʹÓÃÖøÃûµÄjQuery£¬µ«ÊÇÈç¹ûÎÒÃǶԻù´¡µÄ¶«Î÷ÓкܺõÄÀí½â£¬ÄÇÕâЩ¿â±¨¸æ´íÎ󣬻òÕß³öÏÖÎÊÌâÎÒÃÇ¿ÉÒԺܺúܿìµÄÖªµÀ´íÔÚÄÄÀ¸ü¿ìµÄ×ö³ö¸Ä±äʹ³ÌÐòÕý³£ÔËÐС£ (ÔðÈα༣ºadmin) |