I'm working on Laravel version 5.2. I've been trying to get json data from post. I always get empty data. I also have tried to use solutions found on the internet but there's nothing works. for example,
- $request->json()->all();
- Input::get('data');
- $request->get('data');
- $request->data;
- Input::all();
- json_decode(request()->getContent(), true);
- json_decode($request->getContent(), true);
- json_decode(request()->get('payload'));
- json_decode($request->get('payload'));
- $request->input('data');
Here is my javascript code,
$.ajax({
method: 'POST',
url: url,
contentType: 'application/json',
headers: {
'X-CSRF-TOKEN': token
},
data: {'data':'foo'},
success: function(data) {
console.log(data);
}
});
And here is my laravel code,
public function postJsonData(Request $request){
//several methods I am trying to use.
$data = $request->data;
Log::info($data);
return json_encode($data);
}
Now I really have no ideas to move on. I really need help. Thanks for your time.
via Chebli Mohamed