使用 Laravel/ajax 获取 http://localhost:8000/= 404(未找到)

Posted

技术标签:

【中文标题】使用 Laravel/ajax 获取 http://localhost:8000/= 404(未找到)【英文标题】:GET http://localhost:8000/= 404 (Not Found) using Laravel/ajax 【发布时间】:2021-11-21 06:50:18 【问题描述】:

我有以下显示通知的代码:

<div class="position-relative d-inline-block">
                <button class="header-icon btn btn-empty" type="button" id="notificationButton"
                data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <i class="simple-icon-bell"></i>
                    <span class="count">$contrats_expires_count</span>
                </button>
                <div class="dropdown-menu dropdown-menu-right mt-3 position-absolute abc" id="notificationDropdown">
                    
                    @foreach($contrats_expires as $contrats_expires)
                      <a href=" route('getcontrat', $contrats_expires->id_contrat) ">  
                        <div class="d-flex flex-row mb-3 pb-3 border-bottom">

                            <a href=" route('getcontrat', $contrats_expires->id_contrat) ">
                                <img src=" '/castingimages/' . $contrats_expires->photo. ''" 
                                class="img-thumbnail list-thumbnail xsmall border-0 rounded-circle" />
                            </a>
                            <div class="pl-3">
                                <a href=" route('getcontrat', $contrats_expires->id_contrat) ">
                                    <p class="font-weight-medium mb-1">$contrats_expires->nom $contrats_expires->prenom</p>
                                    <p class="text-muted mb-0 text-small">$contrats_expires->numero_projet $contrats_expires->numero_contrat</p>
                                </a>
                            </div>

                        </div>
                    </a>
                  @endforeach    
                </div>
            </div>

所以现在我尝试在单击通知时获取单击通知的 if 并根据此 id 在另一个页面上显示数据。

所以我有以下路线:

Route::group(['middleware' => ['auth','role:account_manager|admin|manager_de_filiale']], function()  
    Route::get('/contrat/id_contrat', [App\Http\Controllers\DashboardController::class, 'getContrat'])->name('getcontrat');
);

以及以下控制器:

 public function getContrat(Rquest $request, $id_contrat)

    if(request()->ajax())
    
       return datatables()->of(Contrat::where('contrats.id_contrat',$id_contrat)->leftjoin('projets_castings', 'contrats.id_contrat', '=', 'projets_castings.id_contrat')->leftjoin('castings', 'castings.id_casting', '=', 'projets_castings.id_casting')->leftjoin('projets','projets.id_projet','=','projets_castings.id_projet')->leftjoin('modele_contrat','modele_contrat.id_modele_contrat','=','contrats.id_modele_contrat')->select('contrats.numero_contrat','castings.nom','modele_contrat.modele_contrat','contrats.date_signature','contrats.lieu_signature','contrats.paye','contrats.date_paiement','contrats.created_at','contrats.actif','projets_castings.type_contrat','projets_castings.id_projet_casting','projets_castings.id_contrat','contrats.date_fin_contrat','projets.numero_projet','projets_castings.type_contrat','contrats.id_contrat')->latest()->get())
       
            ->addColumn('action', function($data)
                $button = '<table><tr><td>';
                        $button .= '<button type="button" name="edit" id="'.$data->id_contrat.'" class="edit btn btn-primary btn-sm">Modifier</button>';
                         
                $button .= '</td><td>';
                  /*$button .= '<button type="button" name="download" id="'.$data->id_contrat.'" class="download btn btn-primary btn-sm">Télécharger</button>';*/

                  $button .='<a href="list_contrats/'.$data->id_contrat.'/download" class="download btn btn-primary btn-sm"  data_id="'.$data->id_contrat.'" >Télécharger</a>';
                         
                $button .= '</td><td>';
                $button .= ' <label  class="switch" >';
                       $button .= '  <input type="checkbox" id="'.$data->id_contrat.'" class="switch selectRow" ';
                 
                        
                
                if ($data->actif == 1) 

                    $button .= "checked";
                

                $button .= '><span class="slider round"></span></label>';
                $button .= '</td></tr></table>';

                return $button;
        )->addColumn('payee', function($data)
               $button1 = '<table><tr><td>';
                $button1 .= ' <label  class="switch" >';
                       $button1 .= '  <input type="checkbox" id="'.$data->id_contrat.'" class="switch selectRow1" ';

                if ($data->paye == 1) 

                    $button1 .= "checked";
                

                $button1 .= '><span class="slider round"></span></label>';
                $button1 .= '</td></tr></table>';

                return $button1;
        )
        ->rawColumns(['action','payee','type_contrat'])
        ->make(true);

       
      $modeles_contrat = Model_Contrat::where('id_filiale',Auth::user()->id_filiale)->latest()->get();

    return view('Contrat_files.contrats')->with('modeles_contrat', $modeles_contrat);
    

我的刀片视图中的以下脚本:

<script>

   $(document).ready(function()
/**************afficher les données **********************/
var $tabledata =   $('.data-table-feature').DataTable(
        destory: true,
        processing: true,
        serverSide: true,
        ajax:
         url: " route('contrat.getContrat') ",
         type: "GET",
        ,
        columns:[
        
         
            data: 'numero_contrat',
            name: 'numero_contrat',
            orderable: false
         ,
         
            data: 'numero_projet',
            name: 'numero_projet',
            orderable: false
         ,
         
            data:'nom',
            name: 'nom'
         ,
         
            data: 'modele_contrat',
            name: 'modele_contrat'
         ,
         
           data:   'date_signature',
           name:   'date_signature'
         ,
         
           data:   'date_fin_contrat',
           name:   'date_fin_contrat'
         ,
          
           data:   'lieu_signature',
           name:   'lieu_signature'
         ,
         
         
            data: 'payee',
            name: 'payee',
            orderable: false
         ,
         
            data: 'date_paiement',
            name: 'date_paiement',
            orderable: false
         ,
         
           data:   'action',
           name:   'action',
             orderable: false
         
        ] 
       );
       );
     </script>

还有以下路线:

Route::group(['middleware' => ['auth','role:account_manager|admin|manager_de_filiale']], function()  
   Route::get('contrat', [App\Http\Controllers\DashboardController::class, 'getContrat'])->name('contrat.getContrat');
);

所以我收到以下错误:GET http://localhost:8000/contrat/189%7D 404 (Not Found)

我不知道我的代码中的错误在哪里,如果您有任何想法,请帮助。

【问题讨论】:

【参考方案1】:

您必须更正刀片文件代码 URL:

 url: " route('contrat.getContrat') ",

 route('contrat.getContrat',[$id_contrat]) 

你在路由和控制器函数$id_contact中有一个参数,所以你需要在URL中传递这个参数。

【讨论】:

谢谢你的回答,但我总是同样的错误【参考方案2】:

试着这样写你的路线:

    Route::group(['middleware' => ['auth','role:account_manager|admin|manager_de_filiale']], function() 
     Route::get('contrat', 'DashboardController@getContrat')->name('contrat.getContrat');

     Route::get('/contrat/id_contrat', 'DashboardController@getContrat')->name('contrat.getcontrat');
    );

这可能有效。

【讨论】:

以上是关于使用 Laravel/ajax 获取 http://localhost:8000/= 404(未找到)的主要内容,如果未能解决你的问题,请参考以下文章

在 Laravel Ajax 请求验证中获取错误字段名称

POST http://localhost:8000/offers-ajax/store 419 (未知状态) Laravel AJAX

在从laravel ajax返回的json中的每个斜杠中获取额外的反斜杠“\”

Laravel AJAX 请求进入路由 api Vue 错误

Laravel 5.1 Ajax 从 Blade View 获取 html

Laravel 5.7 Ajax 发布请求返回 419 状态码