AJax¼¼Êõ

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

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

POSTÇëÇóÓÃÓÚÏò·þÎñÆ÷·¢ËÍÓ¦¸Ã±»±£´æµÄÊý¾Ý¡£POSTÇëÇóµÄÖ÷Ìå¿ÉÒÔ°üº¬·Ç³£¶àµÄÊý¾Ý£¬¶øÇÒ¸ñʽ²»ÏÞ¡£ !DOCTYPE htmlhtmlhead lang=en meta charset=UTF-8 titlePOST/title/headbody h1XMLHttpRequest - AjaxÇëÇó/h1 i

POSTÇëÇóÓÃÓÚÏò·þÎñÆ÷·¢ËÍÓ¦¸Ã±»±£´æµÄÊý¾Ý¡£POSTÇëÇóµÄÖ÷Ìå¿ÉÒÔ°üº¬·Ç³£¶àµÄÊý¾Ý£¬¶øÇÒ¸ñʽ²»ÏÞ¡£

<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>POST</title></head><body> <h1>XMLHttpRequest - AjaxÇëÇó</h1> <input type="button" onclick="XmlPostRequest();" value="Post·¢ËÍÇëÇó" /> <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 XmlPostRequest(){ var xhr = GetXHR(); // ¶¨Òå»Øµ÷º¯Êý xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ // ÒѾ­½ÓÊÕµ½È«²¿ÏìÓ¦Êý¾Ý£¬Ö´ÐÐÒÔϲÙ×÷ var data = xhr.responseText; console.log(data); } }; // Ö¸¶¨Á¬½Ó·½Ê½ºÍµØÖ·----Îļþ·½Ê½ xhr.open('POST', "/test/", true); // ÉèÖÃÇëÇóÍ· xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset-UTF-8'); // ·¢ËÍÇëÇó xhr.send('n1=1;n2=2;'); } </script></body></html>

jquery ajax

jQuery Ìṩ¶à¸öÓë AJAX Óйصķ½·¨¡£

ͨ¹ý jQuery AJAX ·½·¨£¬ÄúÄܹ»Ê¹Óà HTTP Get ºÍ HTTP Post ´ÓÔ¶³Ì·þÎñÆ÷ÉÏÇëÇóÎı¾¡¢HTML¡¢XML »ò JSON - ͬʱÄúÄܹ»°ÑÕâЩÍⲿÊý¾ÝÖ±½ÓÔØÈëÍøÒ³µÄ±»Ñ¡ÔªËØÖС£

•jQuery ²»ÊÇÉú²úÕߣ¬¶øÊÇ´ó×ÔÈ»°áÔ˹¤¡£

•jQuery Ajax±¾ÖÊ XMLHttpRequest »ò ActiveXObject

×¢£º2.+°æ±¾²»ÔÙÖ§³ÖIE9ÒÔϵÄä¯ÀÀÆ÷

1. jQuery.get(...) ËùÓвÎÊý£º url: ´ýÔØÈëÒ³ÃæµÄURLµØÖ· data: ´ý·¢ËÍ Key/value ²ÎÊý¡£ success: ÔØÈë³É¹¦Ê±»Øµ÷º¯Êý¡£ dataType: ·µ»ØÄÚÈݸñʽ£¬xml, json, script, text, html2.jQuery.post(...) ËùÓвÎÊý£º url: ´ýÔØÈëÒ³ÃæµÄURLµØÖ· data: ´ý·¢ËÍ Key/value ²ÎÊý success: ÔØÈë³É¹¦Ê±»Øµ÷º¯Êý dataType: ·µ»ØÄÚÈݸñʽ£¬xml, json, script, text, html3.jQuery.getJSON(...) ËùÓвÎÊý£º url: ´ýÔØÈëÒ³ÃæµÄURLµØÖ· data: ´ý·¢ËÍ Key/value ²ÎÊý¡£ success: ÔØÈë³É¹¦Ê±»Øµ÷º¯Êý¡£4.jQuery.getScript(...) ËùÓвÎÊý£º url: ´ýÔØÈëÒ³ÃæµÄURLµØÖ· data: ´ý·¢ËÍ Key/value ²ÎÊý¡£ success: ÔØÈë³É¹¦Ê±»Øµ÷º¯Êý¡£5.jQuery.ajax(...) ²¿·Ö²ÎÊý£º url£ºÇëÇóµØÖ· type£ºÇëÇó·½Ê½£¬GET¡¢POST£¨1.9.0Ö®ºóÓÃmethod£© headers£ºÇëÇóÍ· data£ºÒª·¢Ë͵ÄÊý¾Ý contentType£º¼´½«·¢ËÍÐÅÏ¢ÖÁ·þÎñÆ÷µÄÄÚÈݱàÂëÀàÐÍ(ĬÈÏ: "application/x-www-form-urlencoded; charset=UTF-8") async£ºÊÇ·ñÒì²½ timeout£ºÉèÖÃÇëÇó³¬Ê±Ê±¼ä£¨ºÁÃ룩 beforeSend£º·¢ËÍÇëÇóÇ°Ö´Ðеĺ¯Êý(È«¾Ö) complete£ºÍê³ÉÖ®ºóÖ´ÐеĻص÷º¯Êý(È«¾Ö) success£º³É¹¦Ö®ºóÖ´ÐеĻص÷º¯Êý(È«¾Ö) error£ºÊ§°ÜÖ®ºóÖ´ÐеĻص÷º¯Êý(È«¾Ö) accepts£ºÍ¨¹ýÇëÇóÍ··¢Ë͸ø·þÎñÆ÷£¬¸æËß·þÎñÆ÷µ±Ç°¿Í»§¶Ë¿Î½ÓÊܵÄÊý¾ÝÀàÐÍ dataType£º½«·þÎñÆ÷¶Ë·µ»ØµÄÊý¾Ýת»»³ÉÖ¸¶¨ÀàÐÍ "xml": ½«·þÎñÆ÷¶Ë·µ»ØµÄÄÚÈÝת»»³Éxml¸ñʽ "text": ½«·þÎñÆ÷¶Ë·µ»ØµÄÄÚÈÝת»»³ÉÆÕͨÎı¾¸ñʽ "html": ½«·þÎñÆ÷¶Ë·µ»ØµÄÄÚÈÝת»»³ÉÆÕͨÎı¾¸ñʽ£¬ÔÚ²åÈëDOMÖÐʱ£¬Èç¹û°üº¬JavaScript±êÇ©£¬Ôò»á³¢ÊÔÈ¥Ö´ÐС£ "script": ³¢ÊÔ½«·µ»ØÖµµ±×÷JavaScriptÈ¥Ö´ÐУ¬È»ºóÔÙ½«·þÎñÆ÷¶Ë·µ»ØµÄÄÚÈÝת»»³ÉÆÕͨÎı¾¸ñʽ "json": ½«·þÎñÆ÷¶Ë·µ»ØµÄÄÚÈÝת»»³ÉÏàÓ¦µÄJavaScript¶ÔÏó "jsonp": JSONP ¸ñʽʹÓà JSONP ÐÎʽµ÷Óú¯Êýʱ£¬Èç "myurl?callback=?" jQuery ½«×Ô¶¯Ìæ»» ? ΪÕýÈ·µÄº¯ÊýÃû£¬ÒÔÖ´Ðлص÷º¯Êý

дһ¸ö×î¼òµ¥µÄÀý×Ó£º

<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title></head><body> <p> <input type="button" onclick="XmlSendRequest();" value='AjaxÇëÇó' /> </p> <script type="text/javascript" src="jquery-1.12.4.js"></script> <script> function JXmlSendRequest(){ $.ajax({ url: ":8000/test/", // ·ÃÎÊurlµØÖ· type: 'GET', // get·½Ê½Ìá½» dataType: 'text',¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ // Êý¾ÝÀàÐÍ success: function(data, statusText, xmlHttpRequest){ // ³É¹¦ºó·µ»ØµÄ½á¹û console.log(data); } }) } </script></body></html>

¿çÓòajax

ÓÉÓÚä¯ÀÀÆ÷´æÔÚͬԴ²ßÂÔ»úÖÆ£¬Í¬Ô´²ßÂÔ×èÖ¹´ÓÒ»¸öÔ´¼ÓÔصÄÎĵµ»ò½Å±¾»ñÈ¡»òÉèÖÃÁíÒ»¸öÔ´¼ÓÔصÄÎĵµµÄÊôÐÔ¡£ËùÒÔajax±¾ÉíÊDz»¿ÉÒÔ¿çÓòµÄ£¬Í¨¹ý²úÉúÒ»¸öscript±êÇ©À´ÊµÏÖ¿çÓò¡£ÒòΪscript±êÇ©µÄsrcÊôÐÔÊÇûÓпçÓòµÄÏÞÖƵġ£

ä¯ÀÀÆ÷ͬԴ²ßÂÔ²¢²»ÊǶÔËùÓеÄÇëÇó¾ùÖÆÔ¼£º

•ÖÆÔ¼£º XmlHttpRequest

•²»ÖÆÔ¼£º img¡¢iframe¡¢scriptµÈ¾ßÓÐsrcÊôÐԵıêÇ©

×¢£º×Ô¼ºÄ£Äâ¿çÓò£¬ÐèÒªÏÖÔÚ×Ô¼ºµçÄÔµÄhostÎļþÀïÃæÌí¼ÓÁ½ÌõÓòÃû£¬ÎÒÕâÀïÌí¼ÓµÄÊÇzhangyanlin.comºÍaylin.comÕâÁ½¸öÓòÃû

Ò»¡¢JSONPʵÏÖ¿çÓòÇëÇó

JSONPÊÇÒ»¸ö·Ç¹Ù·½µÄЭÒ飬ËüÔÊÐíÔÚ·þÎñÆ÷¶Ë¼¯³ÉScript tags·µ»ØÖÁ¿Í»§¶Ë£¬Í¨¹ýjavascript callbackµÄÐÎʽʵÏÖ¿çÓò·ÃÎÊ¡£jsonpÖ»ÄÜͨ¹ýget·½Ê½½øÐпçÓòÇëÇó

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <input type="button" value="Ajax" onclick="DoAjax();"/> <input type="button" value="JsonpAjax" onclick="JsonpAjax();"/> <script src="/statics/jquery-1.12.4.js"></script> <script src=":8002/statics/jquery.cookie.js"></script> <script> function func(arg) { console.log(arg); // Êä³ö½á¹û¾ÍÊÇpython´úÂë¸ø´«¹ýÀ´µÄÁбí[11,22,33,] } function DoAjax() { $.ajax({ url: 'http://alex.com:8002/index', type: 'POST', data: {'k1': 'v1'}, success: function (arg) { console.log(arg); } }); } function JsonpAjax() {// var tag = document.createElement('script');// tag.src = ":8002/index";// document.head.appendChild(tag);// document.head.removeChild(tag); $.ajax({ url: ":8002/index", dataType: 'jsonp', jsonpCallBack: 'func' // ¶Ô¶Ë¸ø·µ»Øº¯ÊýÃû£¬º¯Êý½ÓÊյIJÎÊýÊÇÄÚÈÝ }) } </script></body></html>

¡¡

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

ÍøÓѵãÆÀ