Ïêϸ½â˵thinkphpµÄAJAXÇëÇóÖÐת´ï²ÎÊýµÄÒªÁì
Ëæ×ÅwebÊÖÒյķÉËÙÉú³¤£¬ajaxÊÖÒÕÒѾ³ÉΪÁËweb¿ª·¢ÖеÄÒ»¸ö²»¿É»òȱµÄ²¿·Ö¡£ÔÚÇ°ºó¶ËÊèÉ¢µÄÇéÐÎÏ£¬Ç°¶ËÐèÒªÓëºó¶Ë¾ÙÐÐÊý¾Ý½»»¥£¬¾ÍÐèҪʹÓõ½ajaxÊÖÒÕ¡£
¶øÔÚʹÓÃAJAXÊÖÒÕ¾ÙÐÐÊý¾Ý½»»¥µÄͬʱ£¬²ÎÊýת´ï¾ÍÏÔµÃÓÈΪÖ÷Òª¡£¶øÔÚʹÓÃthinkphpµÄʱ¼ä£¬ÔõÑùÔÚAJAXÇëÇóÖÐת´ï²ÎÊýÄØ£¿±¾ÆªÎÄÕ¾ͽ«Îª¸÷ÈËÏêϸ½â˵thinkphpµÄAJAXÇëÇóÖÐת´ï²ÎÊýµÄÒªÁì¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÏàʶʲôÊÇAJAXÇëÇó¡£AJAX£¨Asynchronous JavaScript and XML£©ÊÇÒ»ÖÖÓÃÓÚ½¨Éè¿ìËÙ¶¯Ì¬ÍøÒ³µÄÊÖÒÕ£¬ÊÇÒ»ÖÖÎÞÐèÖØмÓÔØÕû¸öÍøÒ³µÄÇéÐÎÏÂÄܹ»¸üв¿·ÖÍøÒ³µÄÊÖÒÕ¡£AJAXͨ¹ýÔÚºǫ́ÓëЧÀÍÆ÷¾ÙÐÐÉÙÁ¿Êý¾Ý½»Á÷£¬ÔÊÐíÍøҳʵÏÖÒì²½¸üС£ÕâÒâζ×Å¿ÉÒÔÔÚ²»Ó°ÏìÓû§ÌåÑéµÄÇéÐÎϸüÐÂÒ³ÃæµÄÒ»²¿·Ö¡£
ÔÚthinkphpÖУ¬ÎÒÃÇͨ³£Ê¹ÓÃjQueryÀ´¾ÙÐÐAJAXÇëÇó¡£¹ØÓÚjQueryµÄʹÓÃÒªÁ죬ÕâÀï²»ÔÙ׸Êö¡£ÎÒÃÇÖ±½ÓÀ´¿´ÔõÑùÔÚthinkphpµÄAJAXÇëÇóÖÐת´ï²ÎÊý¡£
ÔÚthinkphpÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃ$_POST»ò$_GETÀ´»ñÈ¡AJAXÇëÇóÖÐת´ïµÄ²ÎÊýÖµ¡£ÏêϸҪÁìÈçÏÂËùʾ£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
1.ʹÓÃ$_POSTÒªÁì»ñÈ¡²ÎÊýÖµ
$.ajax({ url: '/index/index/testpost', data: {'name': 'Tom', 'age': 18}, method: 'POST', success: function(result) { console.log(result); } });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËPOSTÒªÁìÏòЧÀÍÆ÷·¢ËÍÁËÒ»¸öAJAXÇëÇó£¬ÔÚÇëÇóÖÐת´ïÁËÁ½¸ö²ÎÊý£ºnameºÍage¡£ºó¶Ë´úÂë¿ÉÒÔʹÓÃ$_POSTÊý×éÀ´»ñÈ¡ÕâЩ²ÎÊýÖµ£º
public function testpost() { $name = $_POST['name']; $age = $_POST['age']; echo $name . ' ' . $age; }
µÇ¼ºó¸´ÖÆ
2.ʹÓÃ$_GETÒªÁì»ñÈ¡²ÎÊýÖµ
$.ajax({ url: '/index/index/testget?name=Tom&age=18', method: 'GET', success: function(result) { console.log(result); } });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËGETÒªÁìÏòЧÀÍÆ÷·¢ËÍÁËÒ»¸öAJAXÇëÇ󣬲¢ÔÚÇëÇóÖн«²ÎÊýnameºÍageÖ±½Ó½«ÆäÆ´½ÓÔÚURLÖÐת´ï¡£ºó¶Ë´úÂë¿ÉÒÔʹÓÃ$_GETÊý×éÀ´»ñÈ¡ÕâЩ²ÎÊýÖµ£º
public function testget() { $name = $_GET['name']; $age = $_GET['age']; echo $name . ' ' . $age; }
µÇ¼ºó¸´ÖÆ
3.ʹÓÃthinkphpµÄinput()ÒªÁì»ñÈ¡²ÎÊýÖµ
thinkphp¿ò¼ÜÌṩÁËÒ»¸ö±ã½ÝµÄÒªÁ졪¡ªinput()£¬¿ÉÒÔÓÃÀ´»ñÈ¡ËùÓеIJÎÊýÖµ£¬²»¹ÜÊÇPOSTÕÕ¾ÉGETÇëÇ󶼿ÉÒÔʹÓá£Ó÷¨ÈçÏ£º
$.ajax({ url: '/index/index/testinput', data: {'name': 'Tom', 'age': 18}, method: 'POST', success: function(result) { console.log(result); } });
µÇ¼ºó¸´ÖÆ
ºó¶Ë´úÂëʹÓÃinput()À´»ñÈ¡ÕâЩ²ÎÊýÖµ£º
public function testinput() { $name = input('post.name'); $age = input('post.age'); echo $name . ' ' . $age; }
µÇ¼ºó¸´ÖÆ
×ܽá
ÔÚ±¾ÆªÎÄÕÂÖУ¬ÎÒÃÇÏêϸµØ½â˵ÁËthinkphpµÄAJAXÇëÇóÖÐת´ï²ÎÊýµÄÒªÁ죬°üÀ¨Ê¹ÓÃ$_POST¡¢$_GETºÍinput()ÒªÁì»ñÈ¡²ÎÊýÖµ¡£Í¬Ê±£¬ÎÒÃÇҲͨ¹ýʵÀý´úÂëÑÝʾÁËÔõÑùÔÚAJAXÇëÇóÖÐת´ï²ÎÊý£¬²¢ÇÒÔõÑù»ñÈ¡ÕâЩ²ÎÊýÖµ¡£ÐÅÍи÷È˶ÔÕâЩÄÚÈÝÒѾÓÐÁËÒ»¶¨µÄÏàʶ£¬¹ØÓÚ¿ª·¢WebÓ¦ÓóÌÐò¾ßÓÐÒ»¶¨µÄ²Î¿¼¼ÛÖµ¡£
ÒÔÉϾÍÊÇÏêϸ½â˵thinkphpµÄAJAXÇëÇóÖÐת´ï²ÎÊýµÄÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡