jeudi 31 octobre 2019

Laravel always gets empty from JSON post

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,

  1. $request->json()->all();
  2. Input::get('data');
  3. $request->get('data');
  4. $request->data;
  5. Input::all();
  6. json_decode(request()->getContent(), true);
  7. json_decode($request->getContent(), true);
  8. json_decode(request()->get('payload'));
  9. json_decode($request->get('payload'));
  10. $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

Aucun commentaire:

Enregistrer un commentaire