使用带有laravel 5.2的ajax

时间:2016-07-18 22:01:20

标签: ajax laravel-5.2

我想在搜索框中输入内容时使用ajax显示表格。 在生成任何表之前,我只是通过打印搜索的关键字来测试是否到达控制器的ajax调用。但它没有用。我认为问题在于网址。但我不知道可能是什么解决方案。

以下是我的观点:

 <input class="form-control" type="text" name="searched_key"    id="searched_key">

 <div id="live-data"></div>

这是我的ajax:

 <script>
  $(document).ready(function () {
    $("#searched_key").keyup(function () {
        var string = $(this).val();
        $.ajax({
          type: "post",
          url: "form_value",
          data: {searched_key: $string},
          success: function (data) {
              $("#live-data").html(data);
          }
      });

  });

});

这是我的控制器:

class HomeController extends Controller {
 public function get_applicants_info(){

     $key_word = $_POST['searched_key'];

     echo $key_word;  //to test the ajax call
}
}

这是我的路线:

Route::get('/', 'HomeController@index');

Route::post('form','HomeController@store');

Route::post('form_value','HomeController@get_applicants_info');

1 个答案:

答案 0 :(得分:1)

我认为问题出在您的AJAX网址上。而不是直接指定URL尝试使用url()函数(如果您使用的是刀片引擎)。

<script>
  $(document).ready(function () {
    $("#searched_key").keyup(function () {
        var string = $(this).val();
        $.ajax({
          type: "post",
          url: "{{url(form_value)}}", <= change your url to this.
          data: {searched_key: $string},
          success: function (data) {
              $("#live-data").html(data);
          }
      });

  });

});