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

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。