尊龙凯时人生就是博

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
sitemap¡¢ÍøÕ¾µØͼ