【问题标题】:Laravel passing data using ajax to controllerLaravel 使用 ajax 将数据传递给控制器
【发布时间】:2014-12-08 16:09:01
【问题描述】:

如何将此 ajax 调用中的 id 传递给 TestController getAjax() 函数?当我打电话时,网址是 testUrl?id=1

Route::get('testUrl', 'TestController@getAjax');

<script>
    $(function(){
       $('#button').click(function() {
            $.ajax({
                url: 'testUrl',
                type: 'GET',
                data: { id: 1 },
                success: function(response)
                {
                    $('#something').html(response);
                }
            });
       });
    });    
</script>

TestController.php

public function getAjax()
{
    $id = $_POST['id'];
    $test = new TestModel();
    $result = $test->getData($id);

    foreach($result as $row)
    {
        $html =
              '<tr>
                 <td>' . $row->name . '</td>' .
                 '<td>' . $row->address . '</td>' .
                 '<td>' . $row->age . '</td>' .
              '</tr>';
    }
    return $html;
}

【问题讨论】:

    标签: ajax laravel routes


    【解决方案1】:

    最后,我只是将参数添加到 Route::get() 和 ajax url 调用中。我在 getAjax() 函数中将 $_POST['id'] 更改为 $_GET['id'] ,这得到了我的回复

    Route::get('testUrl/{id}', 'TestController@getAjax');
    
    <script>
        $(function(){
           $('#button').click(function() {
                $.ajax({
                    url: 'testUrl/{id}',
                    type: 'GET',
                    data: { id: 1 },
                    success: function(response)
                    {
                        $('#something').html(response);
                    }
                });
           });
        });    
    </script>
    

    TestController.php

    public function getAjax()
    {
        $id = $_GET['id'];
        $test = new TestModel();
        $result = $test->getData($id);
    
        foreach($result as $row)
        {
            $html =
                  '<tr>
                     <td>' . $row->name . '</td>' .
                     '<td>' . $row->address . '</td>' .
                     '<td>' . $row->age . '</td>' .
                  '</tr>';
        }
        return $html;
    }
    

    【讨论】:

    • 我用Input::get("id") 代替$id = $_GET['id']; 反正它工作得很好
    【解决方案2】:

    您的 ajax 方法是 GET,但在控制器中您使用 $_POST 来获取 价值。这是个问题。

    你可以吗

    $id = $_GET['id'];
    

    但是在 Laravel 中,它有一个很好的方法来做到这一点。这是here。您无需担心用于请求的 HTTP 谓词,因为所有谓词都以相同的方式访问输入。

    $id = Input::get("id");
    

    如果需要,您可以过滤请求类型以控制异常。 Docs here

    确定请求是否使用 AJAX

    if (Request::ajax())
    {
        //
    }
    

    【讨论】:

    • 我不需要在$.ajax调用中为url指定路由吗?
    • $.ajax 或 $.post 在这种情况下是相同的
    【解决方案3】:
    #in your controller function    
    public function getAjax()
    {
        #check if request is ajax
        if ($request->ajax()) {
            //your code
        }
    
        return $your_data;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-16
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      相关资源
      最近更新 更多