jsÔõÑùÓëthinkphp5Êý¾Ý½»»¥
½üÄêÀ´£¬Ç°¶ËÊÖÒÕµÄѸËÙÉú³¤ÎªÇ°ºó¶ËÊèÉ¢µÄ¿ª·¢Ä£Ê½ÌṩÁ˸üºÃµÄ¿ÉÄÜÐÔ¡£¶øÔÚ¿ª·¢ÖУ¬javascript£¨¼ò³Æjs£©×÷ΪһÖÖ³£ÓõÄÇ°¶Ë¿ª·¢ÓïÑÔ£¬Ò²³ÉΪÁËÇ°¶Ë¿ª·¢²»¿É»òȱµÄÒ»²¿·Ö¡£ÔÚÕâÖÖÇéÐÎÏ£¬jsÔõÑùÓëºó¶Ë¿ò¼ÜÍ·ÄÔphp5£¨¼ò³Ætp5£©¾ÙÐÐÊý¾Ý½»»¥£¬³ÉΪÁË¿ª·¢Ö°Ô±¹Ø×¢µÄÎÊÌâÖ®Ò»¡£ÔÚ±¾ÎÄÖУ¬ÎÒ½«´Ó¼¸¸ö·½ÃæÏêϸÏÈÈÝjsÓëtp5µÄÊý¾Ý½»»¥ÒªÁì¡£
Ò»¡¢Ç°ºó¶ËÊý¾Ý½»»¥·½·¨
Ç°ºó¶ËÊý¾Ý½»»¥µÄ·½·¨Í¨³£ÓÐÁ½ÖÖ£ºÍ¬²½ºÍÒì²½¡£Í¬²½·½·¨¼´Ç°¶Ë·¢ËÍÇëÇóºóÒªÆÚ´ýºó¶Ë·µ»ØÊý¾ÝºóÔÙ×ö³öÏìÓ¦£»Òì²½·½·¨ÔòÊDz»ÐèÒªÆÚ´ý£¬Ç°¶ËÌᳫÇëÇóºó¿ÉÒÔ¼ÌÐøÏòÏÂÖ´ÐУ¬µÈºó¶ËÊý¾Ý·µ»ØºóÔÙ¾ÙÐд¦Öóͷ£¡£
ÔÚÏÖʵ¿ª·¢ÖУ¬Í¬²½·½·¨ÓÉÓÚÆ俨¶ÙµÈȱÏÝÒѾԽÀ´Ô½ÉÙʹÓá£Òì²½·½·¨Ôò³ÉΪÁËÇ°ºó¶ËÊý¾Ý½»»¥µÄÖ÷Òª·½·¨¡£ÔÚºóÃæµÄÌÖÂÛÖУ¬ÎÒÃÇÖ÷Òª½â˵Òì²½·½·¨ÏÂJSÓëtp5Êý¾Ý½»»¥µÄÒªÁì¡£
¶þ¡¢Ê¹ÓÃajax·½·¨ÊµÏÖÒ첽ͨѶ
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚÒ첽ͨѶÖУ¬×î½¹µãµÄ¾ÍÊÇʹÓÃajax·½·¨ÊµÏÖÇ°ºó¶ËÊý¾ÝͨѶ¡£ajaxÊÇAsynchronous JavaScript and XML£¨Òì²½µÄJavaScriptºÍXML£©µÄËõд£¬ÊÇÒ»ÖÖʹÓÃJavaScriptµÄÒ첽ͨѶÊÖÒÕ¡£Ëü¿ÉÒÔ²»ÖØмÓÔØÕû¸öÒ³Ãæ¾ÍÄܹ»¸üÐÂÒ³ÃæµÄ²¿·ÖÄÚÈÝ¡£
ʹÓÃajaxʱ£¬ÎÒÃÇÐèÒªÔÚÇ°¶Ë²¿·Ö±àдJS´úÂ룬ÔÚºó¶Ë²¿·Ö±àдtp5´úÂ롣ǰ¶Ë·¢ËÍajaxÇëÇóºó£¬ºó¶ËÊÕµ½ÇëÇóºó»á·µ»ØJsonÊý¾Ý£¨Ò²¿ÉÒÔÊÇXMLÃûÌõÄÊý¾Ý£©¡£·µ»ØÊý¾ÝºóÓÉÇ°¶ËJS¾ÙÐд¦Öóͷ£¡£
ÒÔÏÂΪÏÖʵ¿ª·¢ÖеÄÒ»¸ö°¸Àý£¬ÔÚ´Ë»ù´¡ÉÏÏêϸÏÈÈÝJSÓëtp5Òì²½½»»¥µÄÏêϸʵÏÖ¡£
µÚÒ»²½£ºÔÚÇ°¶Ë±àдǰ¶Ë´úÂë
ÎÒÃÇÏÈÔÚÇ°¶Ë±àдһ¸öÉèÖÃÓû§È¨ÏÞµÄÒ³Ãæ¡£Ò³ÃæÖÐÐèҪʵÏÖȨÏÞµÄÔöÌí¡¢É¾³ýÁ½¸ö²Ù×÷¡£ÔÚ´Ë£¬ÎÒÃÇÒÔ¡°ÔöÌí¡±²Ù×÷ΪÀý¾ÙÐÐ˵Ã÷¡£
ÎÒÃÇÊ×ÏÈÐèÒªÔÚÒ³ÃæÉϱàдһ¸ö°´Å¥£¬ÊµÏÖµã»÷°´Å¥ºó·ºÆðÒ»¸öÌîдȨÏÞÐÅÏ¢µÄ±íµ¥¡£Í¬Ê±£¬ÎªÁËÀû±ãչʾȨÏÞÐÅÏ¢£¬ÎÒÃÇ»¹ÐèÒªÔÚÒ³ÃæÉϱàдһ¸ö±í¸ñ£¬Õ¹Ê¾ËùÓÐȨÏÞµÄÐÅÏ¢¡£
ÓÃÒÔÏÂJS´úÂëÌìÉúÒ»¸öHTML±í¸ñ£¬ÊµÏÖȨÏÞÐÅÏ¢µÄչʾ¡£
function getAuthorityTable() { $.ajax({ type: "GET", url: "/index/getAuthorityTable", dataType: "json", success: function (data) { var table = "<table><thead><tr><th>ȨÏÞ±àºÅ</th><th>ȨÏÞÃû</th><th>²Ù×÷</th></thead><tbody>"; for (var i = 0; i < data.length; i++) { table += "<tr><td>" + data[i]["id"] + "</td><td>" + data[i]["authority_name"] + "</td><td><button onclick='deleteAuthority(" + data[i]["id"] + ")'>ɾ³ý</button></td></tr>"; } table += "</tbody></table>"; $("#authorityTable").html(table); } }); }
µÇ¼ºó¸´ÖÆ
ÔÚÕâÀïÎÒÃÇʹÓÃÁËajaxÒì²½»ñÈ¡ºó¶ËÊý¾Ý£¬ÌìÉúÁËÒ»¸öÓÉȨÏÞ±àºÅ¡¢È¨ÏÞÃû¡¢É¾³ý²Ù×÷Èý¸ö²¿·Ö×é³ÉµÄHTML±í¸ñ¡£ÆäÖУ¬getAuthorityTable()ÒªÁì½ç˵ÁËÇ°¶ËÇëÇóURL£¬²¢½«ÇëÇóЧ¹ûÌìÉúHTML±í¸ñչʾÔÚÒ³ÃæÉÏ¡£
µÚ¶þ²½£ºÔÚºó¶Ë±àдtp5´úÂë
ÎÒÃÇÐèÒªÔÚЧÀÍÆ÷¶Ë±àдһ¸öÏìÓ¦¸ÃURLÇëÇóµÄÒªÁì¡£ÔÚtp5ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÿØÖÆÆ÷¼°Ä£×ÓʵÏÖ¡£
ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷Index¿ØÖÆÆ÷ÖУ¬Ìí¼ÓÒ»¸ösetAuthorityÒªÁ죺
public function setAuthority() { $authority_name = input('post.authority_name'); $model = new Authority(); if ($model -> add_authority($authority_name)) { $this -> success('Ìí¼ÓȨÏÞÀÖ³É!'); } else { $this -> error('Ìí¼ÓȨÏÞʧ°Ü£¡'); } }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃinputÒªÁìÎüÊÕPOST²ÎÊý£¬Å²ÓÃAuthorityÄ£×ÓÀàÖеÄadd_authorityÒªÁìÌí¼ÓȨÏÞÐÅÏ¢¡£×îºóʹÓÃtp5ÄÚÖõÄ$this->successºÍ$this->errorÒªÁì·µ»Ø״̬ÐÅÏ¢£¬¼û¸æÇ°¶Ë²Ù×÷Àֳɻòʧ°Ü¡£
µÚÈý²½£ºÔÚÇ°¶ËʹÓÃJS·¢ËÍÇëÇó
ÎÒÃÇÐèÒªÔÚÇ°¶ËJS´úÂëÖУ¬ÊµÏÖµã»÷°´Å¥ºó·¢ËÍÒ»¸öÇëÇó¸øºó¶Ë£¬²¢»ñÈ¡ºó¶Ë·µ»ØµÄÏìÓ¦¡£
ÒÔÌí¼ÓȨÏÞ²Ù×÷ΪÀý£¬ÔÚ°´Å¥µã»÷ÊÂÎñÖÐʵÏִ˲Ù×÷¡£
function addAuthority() { var authority_name = $("#authorityName").val(); $.ajax({ type: "POST", url: "/index/setAuthority", dataType: "json", data: {authority_name: authority_name}, success: function (data) { alert(data.msg); } }); }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËPOST·½·¨ÏòЧÀÍÆ÷·¢ËÍÁËÒ»¸öÇëÇ󣬽«¡°authority_name¡±×÷Ϊ²ÎÊý·¢Ë͵½SetAuthority¿ØÖÆÆ÷ÖС£½Ó×Å£¬Ê¹ÓÃdata.msg·µ»ØÁ˲Ù×÷״̬ÐÅÏ¢¡£
Èý¡¢ÆäËû×¢ÖØÊÂÏî
³ýÁËÒÔÉÏÄÚÈÝ£¬ÉÐÓÐһЩÐèҪעÖصĵ㣺
1.¿çÓòÇëÇó£ºÓÉÓÚajaxÇëÇóÊÇÒì²½µÄ£¬¶øÓòÃû»®·ÖÊÇÇ°ºó¶ËµÄ£¬ÎªÁË°ü¹ÜÇå¾²ÐÔ£¬Ç°¶ËJSÓëºǫ́ЧÀÍÆ÷²»ÔÚͳһ¸öÓòÃûÏ¡£Õâʱ¼äÐèҪ˼Á¿½â¾ö¿çÓòÇëÇóÎÊÌâ¡£³£¼ûµÄ½â¾öÒªÁìÓÐʹÓÃJSONP¡¢ÔÚºó¶ËÌí¼ÓHeaderµÈ¡£
2.CSRF¹¥»÷£ºÓÉÓÚajaxÒì²½ÇëÇó»áµ¼Ö¿çÕ¾ÇëÇóαÔ죨CSRF£©µÄÇå¾²ÎÊÌ⣬tp5ÒѾʹÓÃÁË×Ô´øµÄÌá·ÀCSRF¹¥»÷ÒªÁ죬¿ª·¢Ê±¿ÉÒÔÔÚthinkconfig.phpÎļþÖÐÐÞ¸ÄÏà¹Ø²ÎÊý¡£
3.Ç°ºó¶Ë½»»¥ÑéÖ¤£ºÎªÁË°ü¹ÜϵͳµÄÇå¾²ÐÔ£¬Ç°ºó¶ËÊý¾Ý½»»¥Àú³ÌÖÐÐèÒª¾ÙÐÐÑéÖ¤£¬¿ÉÒÔʹÓÃtp5 ValidatorÀà¾ÙÐÐÏà¹Ø²Ù×÷¡£
4.JSONÊý¾Ý´¦Öóͷ££ºÓÉÓÚajaxÒì²½ÇëÇóʱÐèÒª´¦Öóͷ£JSONÃûÌÃÊý¾Ý£¬Ç°¶ËJS³£Ê¹ÓÃJson.stringify¡¢JSON.parseµÈÒªÁì¾ÙÐÐÏà¹Ø²Ù×÷¡£
×ÜÖ®£¬JSÓëtp5µÄÊý¾Ý½»»¥ÊÇ»ùÓÚajaxµÄÒ첽ͨѶ·½·¨ÊµÏֵġ£Ö»ÒªÃ÷È·JSºÍtp5µÄͨѶ·½·¨£¬Ñ§Ï°ÏìÓ¦µÄ¹¤¾ßºÍÒªÁ죬ÎÒÃǾͿÉÒÔÔ½·¢¸ßЧµØÍê³ÉÍøÕ¾µÄ¿ª·¢¡£Í¬Ê±£¬ÔÚ¿ª·¢Àú³ÌÖУ¬ÎÒÃÇ»¹ÐèҪ˼Á¿Ò»Ð©Çå¾²ÐÔºÍÑéÖ¤ÎÊÌ⡣ϣÍû±¾ÎÄÄܹ»¶Ô¶ÁÕßÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇjsÔõÑùÓëthinkphp5Êý¾Ý½»»¥µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡