AJax¼¼Êõ

ajax֪ʶ AjaxѧϰȫÌ×(×îÈ«×î¾­µä)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2016-12-25 11:00 ÎÒÒªÆÀÂÛ( )

AjaxѧϰȫÌ×(×îÈ«×î¾­µä),ajax¼ò½éAJAX¼´¡°Asynchronous Javascript And XML¡±£¨Òì²½JavaScriptºÍXML£©£¬ÊÇÖ¸Ò»ÖÖ´´½¨½»»¥Ê½ÍøÒ³Ó¦ÓõÄÍøÒ³¿ª·¢¼¼Êõ¡£Ajax²»ÊÇ

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µÄÖ÷ÒªÊôÐÔÓУº

1. Number readyState ״ֵ̬£¨ÕûÊý£©£¬¿ÉÒÔÈ·¶¨ÇëÇó/ÏìÓ¦¹ý³ÌµÄµ±Ç°»î¶¯½×¶Î •0£ºÎ´³õʼ»¯¡£Î´µ÷ÓÃopen()·½·¨ •1£ºÆô¶¯¡£ÒѾ­µ÷ÓÃopen()·½·¨£¬Î´µ÷ÓÃsend()·½·¨ •2£º·¢ËÍ¡£ÒѾ­µ÷ÓÃsend()·½·¨£¬Î´½ÓÊÕµ½ÏìÓ¦ •3£º½ÓÊÕ¡£ÒѾ­½ÓÊÕµ½²¿·ÖÊý¾Ý •4£ºÍê³É¡£ÒѾ­½ÓÊÕµ½È«²¿Êý¾Ý£¬¿ÉÒÔÔÚ¿Í»§¶ËʹÓà 2. Function onreadystatechange µ±readyStateµÄÖµ¸Ä±äʱ×Ô¶¯´¥·¢Ö´ÐÐÆä¶ÔÓ¦µÄº¯Êý£¨»Øµ÷º¯Êý£© 3. String responseText ×÷ΪÏìÓ¦Ö÷Ìå±»·µ»ØµÄÎı¾£¨×Ö·û´®ÀàÐÍ£© 4. XmlDocument responseXML ·þÎñÆ÷·µ»ØµÄÊý¾Ý£¨Xml¶ÔÏó£© 5. Number states ״̬Â루ÕûÊý£©£¬È磺200¡¢404... 6. String statesText ״̬Îı¾£¨×Ö·û´®£©£¬È磺OK¡¢NotFound...

¶þ¡¢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ÇëÇó

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • Java EE£ºAjax¼¼ÊõÔÚÄÄÀ

    Java EE£ºAjax¼¼ÊõÔÚÄÄÀ

    2016-12-25 12:08

  • ¡¾Ajax¡¿¡ª¡ªÑ§Ï°ºóµÄС½á

    ¡¾Ajax¡¿¡ª¡ªÑ§Ï°ºóµÄС½á

    2016-12-25 10:05

  • J2EE¿ª·¢¼¼Êõµã4£ºajax¼¼Êõ

    J2EE¿ª·¢¼¼Êõµã4£ºajax¼¼Êõ

    2016-12-24 17:00

  • AJAX¼¼Êõ½â¶Á

    AJAX¼¼Êõ½â¶Á

    2016-12-24 16:08

ÍøÓѵãÆÀ