6

I am doing ajax request and passing this data

    $.ajax({
  url: "{{URL::to('match')}}/"+ id,
  type: 'PUT',
  // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
  data: {
    match_id : id,
    start_time : newTime,
    competitionId: {{$cid}},
    _token:     '{{ csrf_token() }}'
  }
})

and in laravel trying to get this data as

dd($request->start_time);

but it is not working i am getting null

In chrome developer tools data with ajax request sent correctly this is one simple

match_id:1
start_time:03:00
competitionId:1
_token:9p8plPay7HLvJvMrTgxayEH74Ow6c2D1cli1yU01

all of this was working fine before I moved this site to a new server

have i missed any file ?

11
  • Try getting the parameters via $request->input - according to laravel.com/docs/5.4/requests#retrieving-input that should work regardless of request method. Commented Apr 18, 2017 at 14:09
  • Check the network tab in your browser (Chrome). Commented Apr 18, 2017 at 14:12
  • I have used this method . I have also tried dd( $request->all()); but getting this [] Commented Apr 18, 2017 at 14:13
  • @theAlpha I have given above simple from network tab Commented Apr 18, 2017 at 14:16
  • @MuhammadAwais Can you show us your controller method in full, and relevant routes? Commented Apr 18, 2017 at 14:26

2 Answers 2

8

It works fine after i changed type to Post and then added a field _method: PUT i.e

$.ajax({
      url: "{{URL::to('match')}}/"+ id,
      type: 'POST',
      // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
      data: {
        _method: 'PUT',
        match_id : id,
        start_time : newTime,
        competitionId: {{$cid}},
        _token:     '{{ csrf_token() }}'
      }
    })
Sign up to request clarification or add additional context in comments.

Comments

2

type php artisan route:list

check your route there for example your

Method = put

Uri = match/{match}

Name = match.update

Action = App\Http\Controllers\MatchController@update //your method

Route:

Route::resource('/match', 'MatchController');

this is your ajax call:

$.ajax({
    url: 'match/'+ id, //this is your uri
    type: 'PUT', //this is your method
    data: { match_id:id, start_time:newTime },
    dataType: 'json',
    success: function(response){

    }
});

Your Controller:

public function update(Request $request, $match_id)
{
   if(request()->ajax()){
      $match = Match::find($match_id);
      $validator = Validator::make($request->all(), [
         'start_time'=>'required',
      ]);

      if($validator->passes()) 
      {
        $match->start_time = $request->start_time;
        $match->save();

        return response()->json(['msg'=>'Updated Successfully', 'success'=>true]);
      }
      return response()->json(['msg'=>$validator->errors()->all()]);
    }
}

15 Comments

I am using resource Controller Route::resource('/match', "MatchController");
same I am using Route::resource('/trademark', 'TrademarksController');
If you are using resource route you should have App\Http\Controllers\MatchController@method after your controller your method
so what i am doing wrong ? my route is correct that's why i am getting null or empty array on changing code on server side
dear in ajax type is mentioned to specify method type and i have put there
|

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.