在Laravel中使用AJAX搜索数据

This is my ajax code. I want to type something in a textbox, and the result from database should show at the table.

<script type="text/javascript">
  function retrieve(){
    var country = $('#country_name').val();
    $.ajax({
      type: "POST",
      url: '{{route('dataAjax-country')}}',
      data: {country:country},
      success: function(result){
        $('#ajaxtable').html(result);
      }
    });
  }
</script>

But the data is not changing when I type on my textbox. I have a textbox and a table like this:

{!! Form::text('country_name', '', array('id'=>'country_name', 'onkeyup'=>'retrieve()')) !!}

<tbody id="ajaxtable">
  @foreach($results as $result)
    <tr>
      <td>{{$result->id}}</td>
      <td>{{$result->country_name}}</td>
    </tr>
  @endforeach
</tbody>

And this is my route code, maybe I lack at my route?:

Route::get('asd', array(
        'as' => 'dataAjax-country', 'uses' => 'CompaniesController@dataAjaxCountry'));

And my controller that contain AJAX script that will show at the table:

public function dataAjaxCountry()
{
    echo "<tr><td>asdsad</td></tr>";
}

You are sending a post request but you are using get on your route, you need to change your type to get, it's also useful to log any errors, try:

<script type="text/javascript">
  function retrieve(){
    var country = $('#country_name').val();
    $.ajax({
      type: "get",
      url: '{{route('dataAjax-country')}}',
      data: {country:country},
      success: function(result){
        $('#ajaxtable').html(result);
      },
      error: function(result){
        console.log(result);
      }
    });
  }
</script>

A couple of points, you are trying to POST to a controller you have defined as a GET route. The below should work

<script type="text/javascript">
  function retrieve(){
    var country = $('#country_name').val();
    $.ajax({
      type: "GET",
      url: '/asd',
      data: {country:country},
      success: function(result){
        $('#ajaxtable').html(result);
      }
    });
  }

You need to wrap your <tbody> insde a <table>

I changed this as the route didnt work url: '{{route('dataAjax-country')}}',

It would be very useful to use the developer console in your browser, and check what error the server returns. Most likely, from your code above, are the wrong route method (it's defined for GET, but you are sending a POST request through AJAX), and the fact that you are not sending the session token with your data. Although it's a little old, this article should help you getting started with Laravel and AJAX: Using AJAX in Laravel