weixin_33717117 2016-07-18 22:01 采纳率: 0%
浏览 30

在laravel 5.2中使用Ajax

I want to display a table using ajax when something is typed in the search box. Before generating any table I am just testing that the ajax call is reached to the controller or not by printing the searched keyword. But it is not working. I think the problem is in the url. But I don't know what could be the solution.

Here is my view:

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

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

Here is my 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);
          }
      });

  });

});

Here is my controller:

class HomeController extends Controller {
 public function get_applicants_info(){

     $key_word = $_POST['searched_key'];

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

Here is my routes:

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

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

Route::post('form_value','HomeController@get_applicants_info');
  • 写回答

1条回答 默认 最新

  • weixin_33692284 2016-07-19 05:32
    关注

    I think that the problem is with your AJAX url. Instead of specifying the URL directly try using url() function (In case you are using blade engine).

    <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);
              }
          });
    
      });
    
    });
    
    评论

报告相同问题?