代码之家  ›  专栏  ›  技术社区  ›  Istiaque Ahmed

Laravel yajra datatable:无法从ajax调用控制器检索搜索参数

  •  0
  • Istiaque Ahmed  · 技术社区  · 6 年前

    here :

    视图中的HTML表:

      <form id="search-form" class="form-inline" method="POST" role="form" action="#">
    
    
          <select id="batch" name="batch">
            <option value="">Select</option>
            <option value="22">Skill Level CBE Dec 2018</option>
            <option value="2">Batch 2</option>
    
          </select>
    
          <button class="btn  btn-primary" type="submit">Search</button>
    
      </form>
    
         <table class="table table-striped table-bordered datatable" cellspacing="0" width="100%">
    
                                        <thead>
    
                                            <tr>
    
    
    
                                                <th>{{ getPhrase('S/N')}}</th>
    
                                                <th>{{ getPhrase('Batch')}}</th>
                                                <th>{{ getPhrase('Course')}}</th>
                                                <th>{{ getPhrase('Subject')}}</th>
    
                                                <th>{{ getPhrase('title')}}</th>
    
                                                <th>{{ getPhrase('otq_marks')}}</th>
                                                <th>{{ getPhrase('cr_marks')}}</th>
    
                                                <th>{{ getPhrase('total')}}</th>
                                                <th>{{ getPhrase('average')}}</th>
    
    
    
    
                                            </tr>
    
                                        </thead>
    
    
    
             </table>
    
    
            @section('footer_scripts')
    
    
    
             @include('common.datatables', array('route'=>URL_STUDENT_EXAM_GETATTEMPTS.$user->slug, 'route_as_url' => 'TRUE'))
    
    
            @stop
    

    common.datatables , datatables.blade.php

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
    
         tableObj = $('.datatable').DataTable({
                processing: true,
                serverSide: true,
                    retrieve :true,
                // cache: true,
                type: 'GET',
    
                ajax: {
                 url: '{{ $routeValue }}',
                 data: function (d) {
                     d.batch = $('#batch').val();
    
                 }
             }
    
    
    
         });
    
    
        $('#search-form').on('submit', function(e) {
            tableObj.draw();
            e.preventDefault();
        });
    

    ajax url值$routeValue引用 URL_STUDENT_EXAM_GETATTEMPTS

    当我从 "batch" 下拉列表并按 submit 按钮,对路由进行ajax调用。在浏览器检查工具中,我看到在ajaxurl和我们的 batch 批量 控制器内的参数。

    关于服务器端代码:

    URL\学生\考试\尝试次数 用在刀刃上有价值 PREFIX.'exams/student/get-exam-attempts/'

    而且在 route.php

    Route::get('exams/student/get-exam-attempts/{user_slug}/{exam_slug?}', 'StudentQuizController@getExamAttemptsData');
    

    public function getExamAttemptsData(Request $request,$slug, $exam_slug = '')
        {
    
        //body goes here 
    
        }
    

    我使用了以下所有方法来获得 批量 控制器中的参数无效:

    $request->get('batch')
    $request->query('batch')
    Input::get('batch')
    

    如何检索 批量

    编辑: 顺便说一句,我正在使用 use Illuminate\Http\Request; Request $request 控制器函数参数中的变量

    编辑2: 浏览器检查工具将ajax请求url显示为:

    http://localhost/lcbs/exams/student/get-exam-attempts/myuser123?draw=2列%5B0%5D%5Bdata%5D=0……搜索%5Bregex%5D=false& 批次=22 &1541684388689。

    你看到了吗 批量 但在控制器内部,代码 $request->getQueryString() %2Fexams%2Fstudent%2Fget-exam-attempts%2Fmyuser123

    以及 \URL::current() 显示http://localhost/lcbs/exams/student/get-exam-attempts/myuser123

    也就是说 $request 缺少完整的查询字符串。

    @阿德里安·赫尔南德斯·洛佩兹,我正在粘贴内核.php在这里:

    namespace App\Http;
    
    use Illuminate\Foundation\Http\Kernel as HttpKernel;
    
    class Kernel extends HttpKernel
    {
        /**
         * The application's global HTTP middleware stack.
         *
         * These middleware are run during every request to your application.
         *
         * @var array
         */
        protected $middleware = [
            \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    
        ];
    
        /**
         * The application's route middleware groups.
         *
         * @var array
         */
        protected $middlewareGroups = [
            'web' => [
                \App\Http\Middleware\EncryptCookies::class,
                \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
                \Illuminate\Session\Middleware\StartSession::class,
                \Illuminate\View\Middleware\ShareErrorsFromSession::class,
                \App\Http\Middleware\VerifyCsrfToken::class,
            ],
    
            'api' => [
                'throttle:60,1',
            ],
        ];
    
        /**
         * The application's route middleware.
         *
         * These middleware may be assigned to groups or used individually.
         *
         * @var array
         */
        protected $routeMiddleware = [
            'auth' => \App\Http\Middleware\Authenticate::class,
            'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
            'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
            'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
            'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    
            'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
            'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
            'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
           // 'adminOrGuest' => \App\Http\Middleware\AdminOrGuestMiddleware::class,
    
        ];
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Adrian Hernandez-Lopez    6 年前

    看起来像那个?在Route中,不用于定义可选参数,而是用于定义字符串:

    Route::get('exams/student/get-exam-attempts/{user_slug}/{exam_slug?}', 'StudentQuizController@getExamAttemptsData');
    

    Route::get('exams/student/get-exam-attempts/{user_slug}/{exam_slug}', 'StudentQuizController@getExamAttemptsData');
    

    此外,您发布的查询字符串后面还有一个空格 myyser123 ,这也可以解释这个问题。

    http:// localhost/lcbs/exams/student/get-exam-attempts/myuser123 ?draw=2&columns%5B0%5D%5Bdata%5D=0......search%5Bregex%5D=false&batch=22&_=1541684388689.
    

    url: '{{ $routeValue }}',