×ðÁú¿­Ê±¹ÙÍøµÇ¼

laravel ÅжÏajaxÇëÇó

laravelÊÇÒ»¿îºÜÊÇÊ¢ÐÐµÄ php web¿ª·¢¿ò¼Ü£¬ËüµÄÐí¶à¹¦Ð§ºÍÌØÕ÷¶¼ÉÁ¿ª·¢Ö°Ô±µÄÊÂÇéÔ½·¢ÇáËɺ͸ßЧ¡£ÆäÖУ¬¹ØÓÚÅÐ¶Ï ajax ÇëÇóµÄÖ§³ÖÊÇ laravel µÄÒ»¸öÏÔÖøÌصã¡£Òò´Ë£¬±¾ÎĽ«Ì½ÌÖÔõÑùʹÓà laravel ÅÐ¶Ï ajax ÇëÇó¡£

Ò»¡¢Ê²Ã´ÊÇ AJAX ÇëÇó

ÔÚÉîÈëÌÖÂÛÔõÑùʹÓà Laravel ÅÐ¶Ï AJAX ÇëÇó֮ǰ£¬ÎÒÃÇÐèÒªÏÈÏàʶʲôÊÇ AJAX ÇëÇó¡£AJAX ÊÇ Asynchronous JavaScript and XMLµÄ¼ò³Æ£¬ÌåÏÖÒì²½ JavaScript ºÍ XML¡£AJAXͨ¹ýä¯ÀÀÆ÷ºÍЧÀÍÆ÷Ö®¼äµÄÔÚºǫ́±¬·¢µÄÊý¾Ý½»Á÷£¬ÊµÏÖÁËÒì²½Ò³Ãæ¸üУ¬´Ó¶øÓÅ»¯ÁËÓû§ÌåÑé¡£

¼òÆÓÀ´Ëµ£¬AJAX ÊÇÒ»ÖÖ»ùÓÚÏÖÓÐµÄ Web ±ê×¼µÄÊÖÒÕ£¬¿ÉÒÔͨ¹ýÔÚºǫ́ÓëЧÀÍÆ÷¾ÙÐÐÉÙÁ¿µÄÊý¾Ý½»Á÷£¬ÎÞÐèË¢ÐÂÒ³Ãæ¾ÍÄÜʵÏÖ¾Ö²¿Ë¢Ð¡£ÓÉÓÚʹÓÃÁË AJAX ÊÖÒÕ£¬Ê¹µÃ Web Ó¦ÓþßÓиü¿ìµÄÏìÓ¦ËÙÂÊ£¬ÔöÌíÁ˽»»¥ÐÔ¡£

¶þ¡¢Laravel ÖÐÅÐ¶Ï AJAX ÇëÇóµÄÒªÁì

Laravel Ö§³Öͨ¹ý Request ¹¤¾ßÀ´»ñÈ¡ÇëÇóµÄÀàÐÍ¡¢Â·¾¶ºÍÊý¾ÝµÈÐÅÏ¢¡£ÔÚÅÐ¶Ï AJAX ÇëÇóʱ£¬¿ÉÒÔʹÓà Request ¹¤¾ßÖÐÌṩµÄ isXmlHttpRequest() ÒªÁì¡£

ÏêϸʵÏÖÒªÁìÈçÏ£º

ÔÚ Laravel ¿ØÖÆÆ÷£¨Controller£©ÖУ¬ÎÒÃÇ¿ÉÒÔ»ñÈ¡Ä¿½ñÇëÇóµÄ Request ¹¤¾ß£º

$request = request();

µÇ¼ºó¸´ÖÆ

ʹÓà isXmlHttpRequest() ÒªÁìÀ´ÅжÏÄ¿½ñÇëÇóÊÇ·ñΪ AJAX ÇëÇó£º

if ($request->ajax()) {
  // Ä¿½ñÇëÇóΪ AJAX ÇëÇó
} else {
  // Ä¿½ñÇëÇó²»ÊÇ AJAX ÇëÇó
}

µÇ¼ºó¸´ÖÆ

ÔÚ´úÂëÖУ¬$request->ajax() ¿ÉÓÃÓÚÅжÏÄ¿½ñÇëÇóÊÇ·ñΪ AJAX ÇëÇ󡣸ÃÒªÁì·µ»Ø true »ò false Öµ¡£ÈôÊÇÄ¿½ñÇëÇóÊÇ AJAX ÇëÇó£¬Ôò·µ»Ø true£»²»È»·µ»Ø false¡£

ÐèҪעÖصÄÊÇ£¬ÔÚÖ´ÐÐ AJAX ÇëÇóʱ£¬±ØÐèʹÓà JavaScript ¹¤¾ßÀ´Å²Óà Laravel ¿ØÖÆÆ÷µÄ·ÓÉ¡£ÈôÊÇÔÚä¯ÀÀÆ÷µØµãÀ¸ÖÐÖ±½ÓÊäÈë·Óɵص㣬»òÕßʹÓÃÆäËû·Ç AJAX ÇëÇó·½·¨Å²Ó÷ÓÉ£¬ÄÇô $request->ajax() ÒªÁ콫·µ»Ø false Öµ¡£

Èý¡¢ÆäËû AJAX Ïà¹Ø¹¦Ð§

³ýÁËÖ§³Ö AJAX ÇëÇóµÄÅжÏÍ⣬Laravel »¹ÌṩÁËÆäËûÓÐÓÃµÄ AJAX Ïà¹Ø¹¦Ð§¡£

CSRF ±£»¤

Laravel ×Ô´øµÄ CSRF ±£»¤¿ÉÒÔ±ÜÃâ¿çÕ¾ÇëÇóαÔì¹¥»÷¡£Ê¹Óà AJAX ÊÖÒÕʱÐèҪעÖØ CSRF ±£»¤£¬ÐèÒªÔÚ JavaScript ÖÐÉèÖÃÈ«¾ÖÉèÖãº

<meta name="csrf-token" content="{{ csrf_token() }}">
<script>
  window.Laravel = { csrfToken: '{{ csrf_token() }}' };
</script>

µÇ¼ºó¸´ÖÆ

ÔÚʹÓà AJAX ·¢ËÍÇëÇóʱ£¬ÐèÒªÉèÖÃÇëÇóÍ·µÄ X-CSRF-Token ²ÎÊý£¬ÒÔ±ã Laravel ½«Æäʶ±ðΪÕýµ±ÇëÇó£º

headers: {
  'X-CSRF-TOKEN': Laravel.csrfToken
}

µÇ¼ºó¸´ÖÆ

ÏìÓ¦ JSON ÃûÌÃÊý¾Ý

Laravel ¿ÉÒÔÔÚ¿ØÖÆÆ÷ÖÐÖ±½Ó·µ»Ø JSON ÃûÌõÄÏìÓ¦¡£Ê¹Óà AJAX ÊÖÒÕʱ£¬Í¨³£½ÓÄÉ·µ»Ø JSON ÃûÌÃÊý¾ÝÀ´´¦ÀíÇëÇóÏìÓ¦£¬ÓÉÓÚÕâÖÖÃûÌÿÉÒÔºÜÀû±ãµØ±» JavaScript ºÍÆäËû¿Í»§¶ËÓ¦ÓóÌÐòËùÆÊÎö¡£

ÒÔÏÂÊÇÒ»¸öÏìÓ¦ JSON ÃûÌÃÊý¾ÝµÄ»ù±¾Ê¾Àý£º

return response()->json([
  'name' => 'John',
  'age' => 25,
]);

µÇ¼ºó¸´ÖÆ

ÔÚ JavaScript ÖУ¬¿ÉÒÔʹÓà jQuery µÈ¿âÀ´ÆÊÎö JSON ÃûÌÃÊý¾Ý£º

$.ajax({
  type: "GET",
  url: "/api/user",
  success: function(data){
    console.log(data.name + ' is ' + data.age + ' years old');
  }
});

µÇ¼ºó¸´ÖÆ

ËÄ¡¢×ܽá

ÔÚ±¾ÎÄÖУ¬ÎÒÃÇ̽ÌÖÁËÔõÑùʹÓà Laravel ÅÐ¶Ï AJAX ÇëÇó¡£ÐèҪעÖصÄÊÇ£¬ÔÚʹÓà AJAX ÊÖÒÕʱ£¬ÐèҪ˼Á¿ CSRF ±£»¤ºÍÒì²½ÏìÓ¦Êý¾ÝµÄÃûÌõÈÎÊÌâ¡£Laravel ¶Ô AJAX ÇëÇóÓÐ×ÅÓÅÒìµÄÖ§³Ö£¬Ê¹µÃ Web Ó¦ÓõĿª·¢±äµÃÔ½·¢¼òÆӺ͸ßЧ¡£Í¨¹ýÏàʶºÍÕÆÎÕ Laravel ÖÐ AJAX ÊÖÒÕµÄÏà¹ØÌØÕ÷ºÍ¹¦Ð§£¬¿ª·¢Ö°Ô±Äܹ»¸üºÃµØÓ¦ÓÃÕâÖÖÊÖÒÕ£¬ÎªÓû§Ìṩ¸üºÃµÄÓû§ÌåÑé¡£

ÒÔÉϾÍÊÇlaravel ÅжÏajaxÇëÇóµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ