0

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

9
  • Route::post('test', 'TagsController@test'); php artisan route:list in command line to check if the route registered. Clear the route cache and config if it did not Commented Apr 11, 2018 at 21:44
  • also $.ajax({ method: "POST", url: "some.php", you need to use method not type. Check the manual Commented Apr 11, 2018 at 21:47
  • @Indra You can use type or method. While type is deprecated, it has not been removed, nor does it have a removal estimate. Commented Apr 11, 2018 at 21:55
  • @Ohgodwhy I know, but it's better to not have to update your code every time you update a library. Also better for the poor souls who come after you. I always recommend doing as the manual says. Commented Apr 11, 2018 at 21:57
  • i removed the type and i cheked the route but the same problem Commented Apr 11, 2018 at 22:23

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

i tried this but it tells me : Non-static method Illuminate\Http\Request::ajax() should not be called statically
thanks , it works but with juste when i changed use Illuminate/http/request by use Request directly
0

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;
   }
}

1 Comment

i also tried it but it says : Method wantJson does not exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.