将数据从jQuery发送到laravel控制器

i'm using laravel and i want to receive data from jQuery to my controller to insert it to the database , i tried many methodes but without success this is my script :

$.ajaxSetup({
   headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
});
$.ajax({
   url:'/test',
   type: 'POST',
   dataType: 'JSON',
   data: {
      "name": "Name",
      "color": "red",
    },
});

and the controller :

public function test()
{
  if(Request::ajax()) {
      $data = Input::all();
  }
  dd(json_decode($data));  
}

and finally the Route :

Route::post('/test',[
    'uses' => 'TagsController@test'
]);

it seems ok for me but the result :( : enter image description here

Maybe the problem is in your controller, because you don't tell the server, which table do you want to use to store your data or maybe because of your url.. jquery does not understand what is {{}}

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

$.ajax({
    url: '/test',
    method: 'post',
    data: $('#form-id').serialize(), // prefer use serialize method
    success:function(data){
        console.log(data);          
    }
});

Controller:

use Illuminate\Http\Request;

public function test(Request $request)
{
  if($request::ajax()) {
      $data = $request->color;
  }
  dd(json_decode($data));  
}

I am using serialize because It's so powerful, I don't need to input one by one the field name

I don't know if your controller is used for only retrieve the data from client or you want to use ajax to store your data in database.. so, I am using $request->color to make sure that the data is retrieved from the client side

Add this meta tag in main blade page

<meta name="csrf-token" content="{{ csrf_token() }}">

And instead of old scholl jquery ajax.. just use Axios. it exist inside app.js. simple and very easy. it already detect the csrd-token meta tag. so no need to do in the form or header

change this

$.ajax({

    url: '{{route("test")}}',
    method: 'post',
    data : {
       "name": "Name",
       "color": "red",
    },
    success:function(data){
        console.log(data);          
    }

});

to this

axios.post('{{route("test")}}', {
    "name": "Name",
    "color": "red"
})
.then(function(response) { 
    console.log(response);
});

Your Route must be like this.

Route::post('/test',[
    'uses' => 'TagsController@test'
])->name('test');

Your controller seems fine..

public function test(Request $request)
{
   if($request->wantsJson()) {
      $data = $request::all();
      return $data;
   }
}