ajax¼ò½é
AJAX¼´“Asynchronous Javascript And XML”£¨Òì²½JavaScriptºÍXML£©£¬ÊÇÖ¸Ò»ÖÖ´´½¨½»»¥Ê½ÍøÒ³Ó¦ÓõÄÍøÒ³¿ª·¢¼¼Êõ¡£Ajax²»ÊÇÒ»ÖÖеıà³ÌÓïÑÔ£¬¶øÊÇʹÓÃÏÖÓбê×¼µÄз½·¨¡£AJAX¿ÉÒÔÔÚ²»ÖØмÓÔØÕû¸öÒ³ÃæµÄÇé¿öÏ£¬Óë·þÎñÆ÷½»»»Êý¾Ý¡£ÕâÖÖÒì²½½»»¥µÄ·½Ê½£¬Ê¹Óû§µ¥»÷ºó£¬²»±ØË¢ÐÂÒ³ÃæÒ²ÄÜ»ñÈ¡ÐÂÊý¾Ý¡£Ê¹ÓÃAjax£¬Óû§¿ÉÒÔ´´½¨½Ó½ü±¾µØ×ÀÃæÓ¦ÓõÄÖ±½Ó¡¢¸ß¿ÉÓᢸü·á¸»¡¢¸ü¶¯Ì¬µÄWebÓû§½çÃæ¡£
Ajax°üÀ¨£º
•XHTMLºÍCSS
•Ê¹ÓÃÎĵµ¶ÔÏóÄ£ÐÍ(Document Object Model)×÷¶¯Ì¬ÏÔʾºÍ½»»¥
•Ê¹ÓÃXMLºÍXSLT×öÊý¾Ý½»»¥ºÍ²Ù×÷
•Ê¹ÓÃXMLHttpRequest½øÐÐÒì²½Êý¾Ý½ÓÊÕ
ÀûÓÃAJAX¿ÉÒÔ×ö£º
•×¢²áʱ£¬ÊäÈëÓû§Ãû×Ô¶¯¼ì²âÓû§ÊÇ·ñÒѾ´æÔÚ¡£
•µÇ½ʱ£¬ÌáʾÓû§ÃûÃÜÂë´íÎó
•É¾³ýÊý¾ÝÐÐʱ£¬½«ÐÐID·¢Ë͵½ºǫ́£¬ºǫ́ÔÚÊý¾Ý¿âÖÐɾ³ý£¬Êý¾Ý¿âɾ³ý³É¹¦ºó£¬ÔÚÒ³ÃæDOMÖн«Êý¾ÝÐÐҲɾ³ý¡£
ajaxαÔì
iframe¾ÍÊÇÎÒÃdz£ÓõÄiframe±êÇ©£º<iframe>¡£iframe±êÇ©ÊÇ¿ò¼ÜµÄÒ»ÖÖÐÎʽ£¬Ò²±È½Ï³£Óõ½£¬iframeÒ»°ãÓÃÀ´°üº¬±ðµÄÒ³Ã棬ÀýÈçÎÒÃÇ¿ÉÒÔÔÚÎÒÃÇ×Ô¼ºµÄÍøÕ¾Ò³Ãæ¼ÓÔرðÈËÍøÕ¾»òÕß±¾Õ¾ÆäËûÒ³ÃæµÄÄÚÈÝ¡£iframe±êÇ©µÄ×î´ó×÷ÓþÍÊÇÈÃÒ³Ãæ±äµÃÃÀ¹Û¡£iframe±êÇ©µÄÓ÷¨Óкܶ࣬Ö÷ÒªÇø±ðÔÚÓÚ¶Ôiframe±êÇ©¶¨ÒåµÄÐÎʽ²»Í¬£¬ÀýÈ綨ÒåiframeµÄ³¤¿í¸ß¡£
Òò´Ë£¬iframe±êÇ©¾ßÓоֲ¿¼ÓÔØÄÚÈݵÄÌØÐÔ£¬ËùÒÔ¿ÉÒÔʹÓÃÆäÀ´Î±ÔìAjaxÇëÇó¡£
<!DOCTYPE html><html> <head lang="en"> <meta charset="UTF-8"> <title>αÔìAJAX</title> </head> <body> <div> <p>ÇëÊäÈëÒª¼ÓÔصĵØÖ·£º<span id="currentTime"></span></p> <p> <input id="url" type="text" /> <input type="button" value="Ìá½»" onclick="LoadPage();"> </p> </div> <div> <h3>¼ÓÔØÒ³ÃæλÖãº</h3> <iframe id="iframePosition" style="width: 100%;height: 500px;"></iframe> </div> <script type="text/javascript"> window.onload= function(){ var myDate = new Date(); document.getElementById('currentTime').innerText = myDate.getTime(); }; function LoadPage(){ var targetUrl = document.getElementById('url').value; document.getElementById("iframePosition").src = targetUrl; } </script> </body></html>ÔÀíÊÇÕâÑùµÄ£¬ÉèÖÃÒ»¸öÌá½»°´Å¥£¬ÔÙÉèÖÃÒ»¸öÊäÈë¿ò£¬µ±ÎÒÃÇÊäÈëÒ»¸öÍøÖ·µÄʱºò£¬ÔÚµ±Ç°µÄÒ³Ãæ¼ÓÔØÊäÈëÍøÖ·µÄÒ³ÃæÐÅÏ¢£¬³ÊÏÖÔÚiframe¿òÀÕâÑù¾ÍÄÜ×öµ½²»Ë¢ÐÂURLÀ´Ìá½»²»Í¬µÄÐÅÏ¢¡£
ÔÉúajax
AjaxµÄºËÐÄÊÇXMLHttpRequest¶ÔÏó(XHR)¡£XHRΪÏò·þÎñÆ÷·¢ËÍÇëÇóºÍ½âÎö·þÎñÆ÷ÏìÓ¦ÌṩÁ˽ӿڡ£Äܹ»ÒÔÒì²½·½Ê½´Ó·þÎñÆ÷»ñÈ¡ÐÂÊý¾Ý¡£
Ò»¡¢XMLHttpRequest¶ÔÏó
AjaxµÄºËÐÄÊÇXMLHttpRequest¶ÔÏó(XHR)¡£XHRΪÏò·þÎñÆ÷·¢ËÍÇëÇóºÍ½âÎö·þÎñÆ÷ÏìÓ¦ÌṩÁ˽ӿڡ£Äܹ»ÒÔÒì²½·½Ê½´Ó·þÎñÆ÷»ñÈ¡ÐÂÊý¾Ý¡£
XHRµÄÖ÷Òª·½·¨ÓУº
1. void open(String method,String url,Boolen async) ÓÃÓÚ´´½¨ÇëÇó ²ÎÊý£º method£º ÇëÇó·½Ê½£¨×Ö·û´®ÀàÐÍ£©£¬È磺POST¡¢GET¡¢DELETE... url£º ÒªÇëÇóµÄµØÖ·£¨×Ö·û´®ÀàÐÍ£© async£º ÊÇ·ñÒì²½£¨²¼¶ûÀàÐÍ£©2. void send(String body) ÓÃÓÚ·¢ËÍÇëÇó ²ÎÊý£º body£º Òª·¢Ë͵ÄÊý¾Ý£¨×Ö·û´®ÀàÐÍ£©3. void setRequestHeader(String header,String value) ÓÃÓÚÉèÖÃÇëÇóÍ· ²ÎÊý£º header£º ÇëÇóÍ·µÄkey£¨×Ö·û´®ÀàÐÍ£© vlaue£º ÇëÇóÍ·µÄvalue£¨×Ö·û´®ÀàÐÍ£©4. String getAllResponseHeaders() »ñÈ¡ËùÓÐÏìӦͷ ·µ»ØÖµ£º ÏìӦͷÊý¾Ý£¨×Ö·û´®ÀàÐÍ£©5. String getResponseHeader(String header) »ñÈ¡ÏìӦͷÖÐÖ¸¶¨headerµÄÖµ ²ÎÊý£º header£º ÏìӦͷµÄkey£¨×Ö·û´®ÀàÐÍ£© ·µ»ØÖµ£º ÏìӦͷÖÐÖ¸¶¨µÄheader¶ÔÓ¦µÄÖµ6. void abort() ÖÕÖ¹ÇëÇóXHRµÄÖ÷ÒªÊôÐÔÓУº
¶þ¡¢getÇëÇó
GETÓÃÓÚÏò·þÎñÆ÷²éѯijЩÐÅÏ¢£º
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title></head><body> <h1>XMLHttpRequest - AjaxÇëÇó</h1> <input type="button" onclick="XmlGetRequest();" value="Get·¢ËÍÇëÇó" /> <script src="/statics/jquery-1.12.4.js"></script> <script type="text/javascript"> function GetXHR(){ var xhr = null; if(XMLHttpRequest){ xhr = new XMLHttpRequest(); }else{ xhr = new ActiveXObject("Microsoft.XMLHTTP"); } return xhr; } function XmlGetRequest(){ var xhr = GetXHR(); // ¶¨Òå»Øµ÷º¯Êý xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ // ÒѾ½ÓÊÕµ½È«²¿ÏìÓ¦Êý¾Ý£¬Ö´ÐÐÒÔϲÙ×÷ var data = xhr.responseText; console.log(data); } }; // Ö¸¶¨Á¬½Ó·½Ê½ºÍµØÖ·----Îļþ·½Ê½ xhr.open('get', "/test/", true); // ·¢ËÍÇëÇó xhr.send(); } </script></body></html>Èý¡¢postÇëÇó
¡¡