菜鳥學習laravel框架之ajax請求
阿新 • • 發佈:2018-12-11
1、如果是form表單的,注意_token欄位,form表裡可以寫 {{ csrf_field() }}
或者
<input type="hidden" name="_token" value="'.csrf_token().'">
2、在頁面上新增
<meta name="csrf-token" content="{{ csrf_token() }}">
2.然後在頁面的script標籤中新增
<script type="text/javascript"> $.ajaxSetup({ headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')} }); </script>
3、直接使用PHP語言輸出【博主有點懶,喜歡使用這種】
$.ajax({ url: "{{ url('/appointment_space') }}" , type: 'POST', data: { _token : '<?php echo csrf_token()?>'}, dataType: 'json', success: function(data){ console.log(data.status); }, error: function(xhr, type){ alert('Ajax error!') } });