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 求制作一个个人网页,
    • ¥15 寻涂色内存脚本作者有项目有市场有资源.却技术
    • ¥15 蓝桥杯c51单片机问题
    • ¥15 ajax跨域问题请求修改代码
    • ¥15 python matplotlib
    • ¥15 短信测压+语音,有偿,必须用Python
    • ¥20 COCOS2DX的protobuf协议注册函数问题
    • ¥15 (标签-Pytorch|关键词-Stream)
    • ¥15 求深圳2019年开放数据应用创新大赛的营运车辆数据!
    • ¥15 软件UI界面绘制折线图