如何解决Laravel中的AJAX搜索?

如何解决Laravel中的AJAX搜索?

问题描述:

我试图让AJAX搜索到我的应用程序,但我有一个问题。如何解决Laravel中的AJAX搜索?

如果我做错什么,请纠正我,我是AJAX

我的路线:

Route::get('/retours/{id}/searchpart/{searchquery}', '[email protected]'); 

我的表和搜索表单中,瓦尔被注释掉了,否则我总是得到一个错误,并不能测试。

{!! Form::open(['class' => 'form-horinzontal']) !!} 
    {!! Form::text('search', null, array('required','class'=>'form-control','placeholder'=>'Zoeken in onderdelen','onkeyup' => 'search_data(this.value, "result")')) !!} 
    {!! Form::submit('zoek', array('class'=>'btn btn-info form-control')) !!} 
{!! Form::close() !!} 
<br> 

<table id="myTable" class="tablesorter"> 
    <thead> 
    <tr> 
     <th>Artikelcode</th> 
     <th>Artikelcode verkoop</th> 
     <th>Omschrijving</th> 
     <th>Prijs</th> 
     <th>Actie</th> 
    </tr> 
    </thead> 
    <tbody> 
    @if(isset($resultquery)) 
     @foreach($resultquery as $result) 
      <tr> 
       <td> 
        <a href="{{ url('/parts', $result->id) }}"> 
         {{ $result->artikelcode }} 
        </a> 
       </td> 
       <td> 
        {{--{{ $result->artikelcodeverkoop }}--}} 
       </td> 
       <td> 
        {{--{{ substr($result->omschrijving,0,50) }}--}} 
       </td> 
       <td> 
        {{--€ {{ $result->prijs }}--}} 
       </td> 
       <td> 
        {{--<a href="{{ url('/retours/' .$retour->id . '/addpart/'. $result->id) }}" style="margin-right: 10px;" class="pull-right">--}} 
        <i class="fa fa-plus"></i> 
        Aan bon toevoegen 
        </a> 
       </td> 
      </tr> 
     @endforeach 
    @else 

    @endif 
    </tbody> 
</table> 

而且我当然控制器:

public function searchpart(Request $request, $searchquery){ 

    $data = Parts::where('omschrijving','LIKE', '%' .$searchquery.'%')->get(); 

    return view('retour.updatefill')->with('resultquery',$data); 

enter image description here

AJAX ofcourse:

function search_data(search_value) { 
     $.ajax({ 
      url:'searchpart/' + search_value, 
      method: 'GET' 
     }).done(function(response){ 
      $('#myTable').html(response);   
     }); 
    } 
  • 有时候我500互联网服务器错误

  • 有时我找不到404

我有我这样做非常错误的感觉。

错误的madalin要求: enter image description here

+0

哪里是你'ajax'? –

+0

对不起,我添加了@ParthTrivedi – Rubberduck1337106092

+0

请'控制台(search_value)'你是否一直得到它? –

使用GET变量不是自定义路线

Route::get('/retours/{id}/searchpart', '[email protected]'); 

public function searchpart(Request $request){ 
    $searchquery = $request->get('searchquery'); 
    $data = Parts::where('omschrijving','LIKE', '%' .$searchquery.'%')->get(); 

    return view('retour.updatefill')->with('resultquery',$data); 

AJAX:

function search_data(search_value) { 
     $.ajax({ 
      url:'{{action("[email protected]")}}' , 
      data:{searchquery:search_value} 
      method: 'GET' 
     }).done(function(response){ 
      $('#myTable').html(response);   
     }); 
    } 
+0

你好,谢谢你,{{action}}需要参数什么我应该给它? – Rubberduck1337106092

+0

传递路由'{id}'的id,如'action('UserController @ profile',['id'=> 1]);' – madalinivascu

+0

我仍然收到内部服务器错误...有这个事情与csrf令牌? – Rubberduck1337106092

你应该回到json response到Ajax回调。

试试这个:

public function searchpart(Request $request, $searchquery){ 

    $data = Parts::where('omschrijving','LIKE', '%' .$searchquery.'%')->get(); 

    return response()->json([ 
     'data' => $data 
    ], 200); 
} 

阿贾克斯

.done(function(response){ 
    //do something 
    console.log(response.data);  
}); 
+0

为什么附加一个HTML JSON的页面? – madalinivascu