Ajax¼¼ÊõºËÐÄÊÇXMLHttpRequest¶ÔÏó(¼ò³ÆXHR)£¬ÕâÊÇÓÉ΢ÈíÊ×ÏÈÒýÈëµÄÒ»¸öÌØÐÔ£¬ÆäËûä¯ÀÀÆ÷ÌṩÉ̺óÀ´¶¼ÌṩÁËÏàͬµÄʵÏÖ¡£ÔÚXHR³öÏÖ֮ǰ£¬AjaxʽµÄͨÐűØÐë½èÖúһЩhackÊÖ¶ÎÀ´ÊµÏÖ£¬´ó¶àÊýÊÇʹÓÃÒþ²ØµÄ¿ò¼Ü»òÄÚǶ¿ò¼Ü¡£
XHRµÄ³öÏÖ£¬ÌṩÁËÏò·þÎñÆ÷·¢ËÍÇëÇóºÍ½âÎö·þÎñÆ÷ÏìÓ¦ÌṩÁËÁ÷³©µÄ½Ó¿Ú¡£Äܹ»ÒÔÒì²½·½Ê½´Ó·þÎñÆ÷»ñÈ¡¸ü¶àµÄÐÅÏ¢£¬Õâ¾ÍÒâζ×Å£¬Óû§Ö»Òª´¥·¢Ä³Ò»Ê¼þ£¬ÔÚ²»Ë¢ÐÂÍøÒ³µÄÇé¿öÏ£¬¸üзþÎñÆ÷×îеÄÊý¾Ý¡£
ËäÈ»AjaxÖеÄx´ú±íµÄÊÇXML£¬µ«AjaxͨÐźÍÊý¾Ý¸ñʽÎ޹أ¬Ò²¾ÍÊÇ˵ÕâÖÖ¼¼Êõ²»Ò»¶¨Ê¹ÓÃXML¡£
IE7+¡¢Firefox¡¢Opera¡¢ChromeºÍSafari¶¼Ö§³ÖÔÉúµÄXHR¶ÔÏó£¬ÔÚÕâЩä¯ÀÀÆ÷Öд´½¨XHR¶ÔÏó¿ÉÒÔÖ±½ÓʵÀý»¯XMLHttpRequest¼´¿É¡£
var xhr = new XMLHttpRequest();
alert(xhr); //XMLHttpRequest
Èç¹ûÊÇIE6¼°ÒÔÏ£¬ÄÇôÎÒÃDZØÐ뻹ÐèҪʹÓÃActiveX¶ÔÏóͨ¹ýMSXML¿âÀ´ÊµÏÖ¡£ÔڵͰ汾IEä¯ÀÀÆ÷¿ÉÄÜ»áÓöµ½ÈýÖÖ²»Í¬°æ±¾µÄXHR¶ÔÏ󣬼´MSXML2.XMLHttp¡¢MSXML2.XMLHttp.3.0¡¢MSXML2.XMLHttp.6.0¡£ÎÒÃÇ¿ÉÒÔ±àдһ¸öº¯Êý¡£
- function createXHR() {
- if (typeof XMLHttpRequest != 'undefined') {
- return new XMLHttpRequest();
- } else if (typeof ActiveXObject != 'undefined') {
- var versions = [
- 'MSXML2.XMLHttp.6.0',
- 'MSXML2.XMLHttp.3.0',
- 'MSXML2.XMLHttp'
- ];
- for (var i = 0; i < versions.length; i ++) {
- try {
- return new ActiveXObject(version[i]);
- } catch (e) {
- //Ìø¹ý
- }
- }
- } else {
- throw new Error('ÄúµÄä¯ÀÀÆ÷²»Ö§³ÖXHR¶ÔÏó£¡');
- }
- }
- var xhr = new createXHR();
ÔÚʹÓÃXHR¶ÔÏóʱ£¬ÏȱØÐëµ÷ÓÃopen()·½·¨£¬Ëü½ÓÊÜÈý¸ö²ÎÊý£ºÒª·¢Ë͵ÄÇëÇóÀàÐÍ(get¡¢post)¡¢ÇëÇóµÄURLºÍ±íʾÊÇ·ñÒì²½¡£
xhr.open('get', 'demo.php', false); //¶ÔÓÚdemo.phpµÄgetÇëÇó£¬falseͬ²½
PS£ºdemo.phpµÄ´úÂëÈçÏ£º
//Ò»¸öʱ¼ä
open()·½·¨²¢²»»áÕæÕý·¢ËÍÇëÇ󣬶øÖ»ÊÇÆô¶¯Ò»¸öÇëÇóÒÔ±¸·¢ËÍ¡£Í¨¹ýsend()·½·¨½øÐз¢ËÍÇëÇó£¬send()·½·¨½ÓÊÜÒ»¸ö²ÎÊý£¬×÷ΪÇëÇóÖ÷Ìå·¢Ë͵ÄÊý¾Ý¡£Èç¹û²»ÐèÒªÔò£¬±ØÐëÌînull¡£Ö´ÐÐsend()·½·¨Ö®ºó£¬ÇëÇó¾Í»á·¢Ë͵½·þÎñÆ÷ÉÏ¡£
xhr.send(null); //·¢ËÍÇëÇó
µ±ÇëÇó·¢Ë͵½·þÎñÆ÷¶Ë£¬ÊÕµ½ÏìÓ¦ºó£¬ÏìÓ¦µÄÊý¾Ý»á×Ô¶¯Ìî³äXHR¶ÔÏóµÄÊôÐÔ¡£ÄÇôһ¹²ÓÐËĸöÊôÐÔ£º
ÊôÐÔÃû | ˵Ã÷ |
responseText | ×÷ΪÏìÓ¦Ö÷Ìå±»·µ»ØµÄÎı¾ |
responseXML | Èç¹ûÏìÓ¦Ö÷ÌåÄÚÈÝÀàÐÍÊÇ"text/xml"»ò"application/xml"£¬Ôò·µ»Ø°üº¬ÏìÓ¦Êý¾ÝµÄXML DOMÎĵµ |
status | ÏìÓ¦µÄHTTP״̬ |
statusText | HTTP״̬µÄ˵Ã÷ |
½ÓÊÜÏìÓ¦Ö®ºó£¬µÚÒ»²½¼ì²éstatusÊôÐÔ£¬ÒÔÈ·¶¨ÏìÓ¦ÒѾ³É¹¦·µ»Ø¡£Ò»°ã¶øÒÑHTTP״̬´úÂëΪ200×÷Ϊ³É¹¦µÄ±êÖ¾¡£³ýÁ˳ɹ¦µÄ״̬´úÂ룬»¹ÓÐһЩ±ðµÄ£º
HTTP״̬Âë | ״̬×Ö·û´® | ˵Ã÷ |
200 | OK | ·þÎñÆ÷³É¹¦·µ»ØÁËÒ³Ãæ |
400 | Bad Request | Óï·¨´íÎóµ¼Ö·þÎñÆ÷²»Ê¶±ð |
401 | Unauthorized | ÇëÇóÐèÒªÓû§ÈÏÖ¤ |
404 | Not found | Ö¸¶¨µÄURLÔÚ·þÎñÆ÷ÉÏÕÒ²»µ½ |
500 | Internal Server Error | ·þÎñÆ÷Óöµ½ÒâÍâ´íÎó£¬ÎÞ·¨Íê³ÉÇëÇó |
503 | ServiceUnavailable | ÓÉÓÚ·þÎñÆ÷¹ýÔØ»òά»¤µ¼ÖÂÎÞ·¨Íê³ÉÇëÇó |
ÎÒÃÇÅжÏHTTP״ֵ̬¼´¿É£¬²»½¨ÒéʹÓÃHTTP״̬˵Ã÷£¬ÒòΪÔÚ¿çä¯ÀÀÆ÷µÄʱºò£¬¿ÉÄܻ᲻̫һÖ¡£
- addEvent(document, 'click', function () {
- var xhr = new createXHR();
- xhr.open('get', 'demo.php?rand=' + Math.random(), false); //ÉèÖÃÁËͬ²½
- xhr.send(null);
- if (xhr.status == 200) { //Èç¹û·µ»Ø³É¹¦ÁË
- alert(xhr.responseText); //µ÷³ö·þÎñÆ÷·µ»ØµÄÊý¾Ý
- } else {
- alert('Êý¾Ý·µ»Øʧ°Ü£¡×´Ì¬´úÂ룺' + xhr.status + '״̬ÐÅÏ¢£º' + xhr.statusText);
- }
- });
ÒÔÉϵĴúÂëÿ´Îµã»÷Ò³ÃæµÄʱºò£¬·µ»ØµÄʱ¼ä¶¼ÊÇʱʱµÄ£¬²»Í¬µÄ£¬ËµÃ÷¶¼ÊÇͨ¹ý·þÎñÆ÷¼°Ê±¼ÓÔػصÄÊý¾Ý¡£ÄÇôÎÒÃÇÒ²¿ÉÒÔ²âÊÔÒ»ÏÂÔÚ·ÇAjaxÇé¿öϵÄÇé¿ö£¬´´½¨Ò»¸ödemo2.phpÎļþ£¬Ê¹Ó÷ÇAjax¡£//PS£ºÈç¹ûûÓÐÏò·þÎñÆ÷¶Ë·¢ËÍ,firebugÎÞ·¢ËÍÌáʾ£¬Èç¹ûÓÐsend()·½·¨£¬Ôòfirebug»áÌáʾÒÑ·¢ËÍ//PS£ºÍ¨¹ýµã»÷ʼþ£¬²»¶ÏµÄÏò·þÎñÆ÷·¢ËÍÇëÇó£¬È»ºó·þÎñÆ÷»áʱʱµÄ·µ»Ø×îеÄÊý¾Ý£¬¾ÍÊÇAjax¹¦ÄÜ//PS£ºIEä¯ÀÀÆ÷µÚÒ»´Î»áÏò·þÎñÆ÷¶ËÇëÇ󣬻ñÈ¡×îÐÂÊý¾Ý£¬¶øµÚ¶þ´ÎËü¾ÍĬÈÏ»ñÈ¡µÄ»º´æÊý¾Ý£¬µ¼ÖÂÊý¾Ý²»ÊÇ×îеÄ//PS£ºÔõô´¦Àí»º´æ£¿¿ÉÒÔʹÓÃJSËæ»ú×Ö·û´®
- <script type="text/javascript" src="base.js"></script>
- <script type="text/javascript">
- addEvent(document, 'click', function () {
- alert("<?php echo Date('Y-m-d H:i:s')?>");
- });
- </script>
±¾Õ¾»¶ÓÈκÎÐÎʽµÄתÔØ£¬µ«ÇëÎñ±Ø×¢Ã÷³ö´¦£¬×ðÖØËûÈËÀͶ¯³É¹û
תÔØÇë×¢Ã÷£º ÎÄÕÂתÔØ×Ô£º°®Ë¼×ÊÔ´Íø http://www.aseoe.com/show-12-318-1.html
תÔØÇë×¢Ã÷£º ÎÄÕÂתÔØ×Ô£º°®Ë¼×ÊÔ´Íø http://www.aseoe.com/show-12-318-1.html