从ajax获取参数到控制器Laravel

I want to pass value of id to controller in Laravel

my Ajax Code:

$(document).ready(function () {


            $(document).on('click', '.brnch_clk', function () {
                //alert('ok')
                var branchid = $(this).data('branch');
                $('#txt_branchid').val(branchid);
                alert($(this).data('branch'));

                $.ajax({
                    'url': 'search',
                    'type': 'GET',
                    'data': {id: branchid},

                    success: function(response){ // What to do if we succeed
                        if(data == "success")
                            alert(response);
                    },
                    error: function(response){
                        alert('Error'+response);
                    }



                });
            });

        });

My Controller:

public function search(Request $request)
{
    $member = $request->get('id');
    return json_encode($member);
}

My Route:

Route::get('search', 'ShowstaffController@search');

My Link:

<a href="{{URL('search')}}" class="brnch_clk"
    data-branch="{{$value->branch_id}}"> {{$value->Branch_Name}}
</a>

How can i get parametter id from ajax to controller??

You can not send GET value in data. Send it in the URL as a query string. Check this code:

$(document).ready(function () {


        $(document).on('click', '.brnch_clk', function (e) {
            e.preventDefault();

            //alert('ok')
            var branchid = $(this).data('branch');
            $('#txt_branchid').val(branchid);
            //alert($(this).data('branch'));

            $.ajax({
                'url': 'search?id='+branchid,
                'type': 'GET',
                'data': {},

                success: function(response){ // What to do if we succeed
                    if(data == "success")
                        alert(response);
                },
                error: function(response){
                    alert('Error'+response);
                }



            });
        });

    });

You should get the parameter from query string using query method.

$request->query('id');

I suppose 'url': 'search?id='+branchid in place of 'url': 'search' should force JQuery to send data as GET parameters. Otherwise, data is sent as POST parameters.

The same thing happened to me, I used all solutions, but could not get the result.

Here what I did,

  • Created a formData object
  • append the param id
  • passed form object to ajax data attribute
  • retrieve the value in laravel controller using $request->get('id');

Here jQuery Snippet

 var branchid = $(this).data('branch');
 var form = new formData();
 form.append('id',branchid)
                $.ajax({
                    'url': 'search',
                    'type': 'GET',
                    'data': form,

                    success: function(response){ // What to do if we succeed
                        if(data == "success")
                            alert(response);
                    },
                    error: function(response){
                        alert('Error'+response);
                    }
                });

Laravel Controller

public function search(Request $request)
{
 $id = $request->get('id');
}

NOTE: You don't need to encode json, Laravel by default send response in JSON format.