AJax¼¼Êõ

[Öö¥] ´ÓÁ㿪ʼѧϰWEBÇ°¶ËÖ®Êý¾Ý½»»¥£¨Ajax£©(3)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-06-19 13:03 ÎÒÒªÆÀÂÛ( )

ÈçÐè»ñµÃÀ´×Ô·þÎñÆ÷µÄÏìÓ¦£¬Ê¹Óà XMLHttpRequest ¶ÔÏóµÄ responseText »ò responseXML ÊôÐÔÀ´»ñÈ¡·µ»ØµÄÊý¾Ý¡£ responseText »ñµÃ×Ö·û´®ÐÎʽµÄÏìÓ¦Êý¾Ý¡£ responseXML »ñµÃ XML ÐÎʽµÄÏìÓ¦Êý¾Ý¡£ AjaxÇëÇó Íê³ÉÒ»¸ö

ÈçÐè»ñµÃÀ´×Ô·þÎñÆ÷µÄÏìÓ¦£¬Ê¹Óà XMLHttpRequest ¶ÔÏóµÄ responseText »ò responseXML ÊôÐÔÀ´»ñÈ¡·µ»ØµÄÊý¾Ý¡£
responseText »ñµÃ×Ö·û´®ÐÎʽµÄÏìÓ¦Êý¾Ý¡£
responseXML »ñµÃ XML ÐÎʽµÄÏìÓ¦Êý¾Ý¡£

AjaxÇëÇó
Íê³ÉÒ»¸öAjaxÇëÇó´óÖ·ÖΪÒÔϼ¸¸ö²½Ö裺

ʾÀý£º

> ></title> <script> window.onload = btn = document.querySelector("button"); btn.onclick = ajax; /*1.´´½¨XMLHttpRequest¶ÔÏó*/ if(window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari ä¯ÀÀÆ÷Ö´ÐдúÂë ajax = new XMLHttpRequest(); } else { // IE6, IE5 ä¯ÀÀÆ÷Ö´ÐдúÂë ajax = new ActiveXObject("Microsoft.XMLHTTP"); } /*ÉèÖÃÇëÇó·½Ê½ºÍµØÖ· ĬÈÏÊÇÒì²½*/ ajax.open("GET", "changeTitle.php"); /*·¢ËÍÇëÇó*/ ajax.send(); /*×¢²á»Øµ÷º¯Êý*/ ajax.onreadystatechange = (ajax.readyState == 4 && ajax.status == 200) { document.querySelector("h1").innerHTML = ajax.responseText; } } } } > > </html>

php´úÂ룺

<?php header('content-type:text/html; charset= utf-8');

ÕâÀïдͼƬÃèÊö

´ÓÉÏͼ¿ÉÒÔ¿´³ö£¬ajaxÇëÇó²¢Ã»ÓÐË¢ÐÂÒ³Ãæ¡£

¸ø·þÎñÆ÷´«²ÎÊý

getÇëÇó
ÔÚopen£¨£©·½·¨ÖÐÖ±½Ó½«²ÎÊýÆ´½ÓÉÏÈ¥
postÇëÇóÐèÒªÏÈÉèÖÃÇëÇóÍ·£¬È»ºóÔÚsend£¨£©·½·¨ÖÐÌí¼Ó²ÎÊý

ʾÀý£º

> >="text/javascript"> window.onload = function() { document.querySelector(xmlHttp; if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } var userName = document.querySelector("#userName").value; // xmlHttp.open("GET", "showParameter.php?userName=" + userName); // xmlHttp.send(); xmlHttp.open("POST", "showParameter.php"); xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlHttp.send("userName="+ userName); xmlHttp.onreadystatechange = (xmlHttp.readyState == 4 && xmlHttp.status == 200) { document.querySelector("h1").innerHTML = xmlHttp.responseText; } } } } > ====>

php´úÂ룺

<?php header('content-type:text/html; charset= utf-8'); //echo $_GET["userName"]; echo $_POST["userName"]; ?>

ÕâÀïдͼƬÃèÊö

½âÎö·µ»ØµÄjsonÊý¾Ý
JSON(JavaScript Object Notation),ÊÇECMAScriptµÄ×Ó¼¯,×÷ÓÃÊǽøÐÐÊý¾ÝµÄ½»»»,¶øÇÒÓÉÓÚÓï·¨¸üΪ¼ò½à,ÍøÂç´«Êä,ÒÔ¼°»úÆ÷½âÎö¶¼¸üΪѸËÙ.

Óï·¨¹æÔò:

Êý¾ÝÀàÐÍ:

// ¶ÔÏó { "name":"yzq", "age":"23", "sex":"true", } // Êý×é [ { "name":"yzq", "age":"16" }, { "name":"yuzhiqiang", "age":"23" } ]

json×Ö·û´®Óëjson¶ÔÏóµÄת»»

JSON.parse()·½·¨:½«JSON×Ö·û´®×ª»¯ÎªJavaScript¶ÔÏó
JSON.stringify()·½·¨:½«JavaScript¶ÔÏó,ת»¯ÎªJSON×Ö·û´®
ÓÉÓÚÀÏʽIE(8ÒÔÏÂ)ä¯ÀÀÆ÷ÖÐûÓÐJSON¶ÔÏó,ͨ¹ýµ¼ÈëJSON2.js¿ò¼Ü¼´¿É½â¾ö,¿ò¼Ü»ñÈ¡µØַΪ:JSON2.js

ʾÀý£º

> ></title> <script> window.onload = function() { document.querySelector(xmlHttp; if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest } else { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlHttp.open("GET", "json.php"); xmlHttp.send(); xmlHttp.onreadystatechange = (this.readyState == 4 && this.status == 200) { /*½«json×Ö·û´®×ª»»Îªjs¶ÔÏó*/ var per = JSON.parse(xmlHttp.responseText); var p=document.querySelectorAll("p"); p[0].innerHTML = "ÐÕÃû:" + per.name + "ÄêÁ䣺" + per.age; /*½«js¶ÔÏóת»»Îªjson×Ö·û´®*/ p[1].innerHTML=JSON.stringify(per); } } } } > > > </html>

¡¡

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

Ïà¹ØÎÄÕÂ
  • Ajaxʽ¸»¿Í»§¶ËÔÚѧϰ¹ÜÀíϵͳµÄÓ¦ÓÃÑо¿

    Ajaxʽ¸»¿Í»§¶ËÔÚѧϰ¹ÜÀíϵͳµÄÓ¦ÓÃÑо¿

    2017-06-19 13:04

  • ¡¶Ext JS 3.2 ѧϰָÄÏ¡·(Learning Ext JS 3.2)Ó¢ÎÄÎÄ×Ö°æ/¸üÐÂÔ´´úÂë[PDF]

    ¡¶Ext JS 3.2 ѧϰָÄÏ¡·(Learning Ext JS 3.2)Ó¢ÎÄÎÄ×Ö°æ/¸üÐÂÔ´´ú

    2017-06-18 16:04

  • Vue.jsѧϰϵÁÐ

    Vue.jsѧϰϵÁÐ

    2017-06-18 12:01

  • Ç°¶Ëѧϰ1¡ª¡ªBootstrap jquery ajax

    Ç°¶Ëѧϰ1¡ª¡ªBootstrap jquery ajax

    2017-06-17 16:06

ÍøÓѵãÆÀ
²