weixin_33725807 2019-09-30 12:50 采纳率: 0%
浏览 31

Laravel Ajax发布数据

I have an eCommerce shop where I have to filter products by its categories.

I have to post an array of all filters in the controller, but I am getting this error

 "message": "",
    "exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
    "file": "C:\\xampp\\htdocs\\web_front\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\RouteCollection.php",
    "line": 179

Here is my code:

 <script>
    $(document).ready(function () {
        var categories = [];
        $('input[name="cat[]"]').on('change', function (e) {
            e.preventDefault();
            categories = []; // reset
            $('input[name="cat[]"]:checked').each(function()
            {
                categories.push($(this).val());
            });



            $.ajax({

                type:"GET",

                url:'advanced_filter/' + categories,


                success:function(data){

                    console.log(data);
                },
                error: function(xhr,errmsg,err)
                {
                    console.log(xhr.responseText);
                }
            });
        });
    });

</script>

web.php

Route::get('advanced_filter/{filters}', 'HomepageController@advanced_filter')->name('advanced_filter');


 public function advanced_filter($filters)
    {
dd($filters);
}

I am trying to show all the filters, so I can make a query to get all the products based on filters. I have created the script where I get all filters and want to post it in the controller. Please, can you see this? Thank you

  • 写回答

1条回答 默认 最新

  • weixin_33716941 2019-09-30 15:22
    关注

    first of all :

    "exception":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
    

    above exeption occur when route is not found . NotFoundHttpException has status 404

    in your code you are passing array of categories in route params , which is syntactically wrong , so thats why failed to find route .

    Route::get('advanced_filter', 'HomepageController@advanced_filter')->name('advanced_filter');
    

    from frontend side : pass categories as query parameters

    url will be :

    /advanced_filter?queryParameter= ids of selected categories

    /advanced_filter?categories=1,3,7

    your ajax function will be :

    $.ajax({
         type:"GET",
         url:`/advanced_filter?categories=${categories.join(',')}`,
         success:function(data){
                 console.log(data);
          },
          error: function(xhr,errmsg,err){
                 console.log(xhr.responseText);
          }
       });
    

    in your controller :

    use Illuminate\Http\Request;
    
    public function advanced_filter(Request $request)
    {
        $filter_categories=[];
        if($request->has('categories')){
          $filter_categories=$request->query('categories');
        }
    
        /* you can pass extra query parameter like sortBy,sortOrder,limit  in url 
         `/advanced_filter?categories=1,3,7&limit=24&sortBy=name&sortOrder=desc`
     */
    
         $sortBy=$request->has('sortBy')?$request->query('sortBy'):'id';
         $sortOrder=$request->has('sortOrder')?$request->query('sortOrder'):'desc';
         $limit = $request->has('limit')?$request->has('limit'):12;
    
         /* assuming you have models with relations */
    
         $query = Products::query();
         $query->with('categories');
         if(count($filter_categories)>0){
            $query->whereHas('categories', function ($q) use ($filter_categories) {
                $q->whereIn('categories.id',$filter_categories);
            });
         }
         $query->orderBy($sortBy,$sortOrder);
         $products = $query->paginate($limit);
         return response()->json($products);
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?