简体   繁体   English

从ajax获取参数到控制器Laravel

[英]Get parameter from ajax to controller Laravel

I want to pass value of id to controller in Laravel我想将 id 的值传递给 Laravel 中的控制器

my Ajax Code:我的 Ajax 代码:

$(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??如何从ajax获取参数ID到控制器?

You can not send GET value in data .您不能在data发送GET值。 Send it in the URL as a query string.URL中将其作为查询字符串发送。 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);
                }



            });
        });

    });

您应该使用query方法从query string获取参数。

$request->query('id');

I suppose 'url': 'search?id='+branchid in place of 'url': 'search' should force JQuery to send data as GET parameters.我想'url': 'search?id='+branchid代替'url': 'search'应该强制 JQuery 将data作为 GET 参数发送。 Otherwise, data is sent as POST parameters.否则,数据将作为 POST 参数发送。

The same thing happened to me, I used all solutions, but could not get the result.同样的事情发生在我身上,我使用了所有解决方案,但无法得到结果。

Here what I did,这是我所做的,

  • Created a formData object创建了一个formData对象
  • append the param id附加参数id
  • passed form object to ajax data attribute表单对象传递给 ajax数据属性
  • retrieve the value in laravel controller using $request->get('id') ;使用$request->get('id')检索Laravel 控制器中的值;

Here jQuery Snippet这里是 jQuery 代码段

 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 Laravel 控制器

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.注意:您不需要编码 json,Laravel 默认以 JSON 格式发送响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM