Laravel - 使用ajax

一,前台模板文件  index.blade.php

<!DOCTYPE html>
<meta name="_token" content="{{ csrf_token() }}"/>
<script src="{{ asset(‘js/jquery-1.9.0.js‘) }}"></script>
<body>
   <button onclick="go()">发送</button>
   <script>
    function go() {
        console.log(123)
        $.ajax({
            type: ‘POST‘,
            url: ‘article/add‘,
            data: { name:‘武卡卡‘,age:‘18‘ },
            dataType: ‘json‘,
            headers: {
            ‘X-CSRF-TOKEN‘: $(‘meta[name="_token"]‘).attr(‘content‘)
            },
            success: function(data){
              console.log(data);
            },
            error: function(xhr, type){
              alert(‘Ajax error!‘)
            }
        });
    }
   </script>
</body>
<html>
 

二,路由文件  web.php

Route::post(‘article/add‘,‘‘);
 

三,控制器文件  ArticleControlle.php

public function add(Request $req){
       return $req->all();
}