lundi 7 novembre 2016

Internal Server Error, when sending an ajax request, only when my controller call another method

I keep getting an internal server error when I send an ajax request, but this happens only when I call another method in my controller. The ajax request works perfectly when I don't call an external method, otherwise, I get a 500 (Internal Server Error) Here is my code :

Route:

Route::post('/test', 'TestController@test');

Controller:

public function theRequest(Request $request){
    return $request->all();
}

public function test(Request $request){
    if($request->isMethod('post'))
    return response()->json(['response' => $this->theRequest()]);
}

Ajax:

$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $.ajax({
  url: '/test',
  type: 'post',
  data:{
  _token: $('input[name=_token]').val(),
  firstName: $('input[name=firstName]').val(),
  lastName: $('input[name=lastName]').val()
        }
   }).done(function (data) {
        console.log(data);
   })

But when I change my controller to this :

public function test(Request $request){
    if($request->isMethod('post'))
    return response()->json(['response' => $request->all()]);
}

It works perfectly !



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire