无法在控制器中获取从ajax发送的任何数据

我有一条路线,


Route::post('/shop', 'ShopController@index');

Route::resource('/shop', 'ShopController')->parameters(['shop' => 'slug']);

我想通过价格范围过滤产品。


这是我的 :


filter_data();

        var sliderrange = $('#slider-range');

        var amountprice = $('#amount');

        function filter_data() {

            var  min_price = $("#min_price").val();

            var  max_price = $("#max_price").val();

            console.log(min_price);

            $.ajax({ url:"/shop", method:"POST",

                data:{ min_price:min_price, max_price:max_price,},

                success:function (data) { },

            });

        };

这是控制器:


    public function index(Request $request)

{

    $data = $request->input('min_price');

    print_r($data);


}

在视图中这是返回一个空数组。


素胚勾勒不出你
浏览 122回答 3
3回答

哔哔one

此方法用于获取所有数据   public function index(Request $request)    {        $data = $request->all();        print_r($data);    }

三国纷争

你调用了两个 post metho,url 是 /shopRoute::post('/shop', 'ShopController@index'); // this is method indexRoute::resource('/shop', 'ShopController') //this called store method.因此,当您调用/shop方法时,它会调用 ShopController 的存储方法。删除Route::post('/shop', 'ShopController@index');并使用存储方法。 public function store(Request $request){    $data = $request->input('min_price');    print_r($data);}

德玛西亚99

在 Js 中:&nbsp; var&nbsp; min_price = $("#min_price").val();&nbsp; &nbsp; var&nbsp; max_price = $("#max_price").val();&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url:"/shop_get",&nbsp; &nbsp; &nbsp; &nbsp; method:"POST",&nbsp; &nbsp; &nbsp; &nbsp; data:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "min_price":min_price,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "max_price":max_price,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "_token":"{{csrf_token()}}"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; success:function(response){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(response);&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp;error:function(errors){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(errors);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });在控制器中:public function index(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; echo "<pre>";&nbsp; &nbsp; &nbsp; &nbsp; print_r($request->all());&nbsp; &nbsp; }在路线中:Route::post('/shop_get', 'ShopController@index');
打开App,查看更多内容
随时随地看视频慕课网APP