想要在没有页面刷新的情况下在 laravel 中填充 html 表中的数据

问题描述 投票:0回答:0

这是我的控制器代码:

public function index()`**
{
    $user = Auth::user();
    $client_id = $user->client_id;
    $units = Unit::where('cid', $client_id)->get();
    return response()->json(['unit',$units]);
}

这段代码的结果:

["unit",[{"id":1,"cid":"hamz0865","unitname":"Jacqurad","unitcontact":"1010101","unitaddress":"Korangi","updated_at":"2023-04-11T15:27:47.000000Z","created_at":"2023-04-11T12:48:41.000000Z"},{"id":2,"cid":"hamz0865","unitname":"asdasd","unitcontact":"asdasdasd","unitaddress":"asdasd","updated_at":"2023-04-11T12:50:23.000000Z","created_at":"2023-04-11T12:50:23.000000Z"}]]>

我想在文件名 unitlst.blade.php 的 html 表中显示数据

这是我的桌子:

<table class="table" id="unitstable">
    <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">CID</th>
            <th scope="col">UNIT NAME</th>
            <th scope="col">UNIT CONTACT</th>
            <th scope="col">UNIT ADDRESS</th>
            <th scope="col">ACTION</th>                   
        </tr>
    </thead>
 <tbody>      
 </tbody>

阿贾克斯:

$(document).ready(function(){
    function getData()
    {
        $.ajax({
            type:"GET",
            url:"{{route('units.index')}}",
            dataType:"json",
            success: function(response)
            {
                var tableBody = $('#unitstable tbody');
                tableBody.empty();
                $.each(response, function(index, unit) {
                    var row = $('<tr>');
                    row.append($('<td>').text(unit.id));
                    row.append($('<td>').text(unit.unitname));
                    row.append($('<td>').text(unit.unitcontact));
                    row.append($('<td>').text(unit.unitaddress));
                    tableBody.append(row);
                });
            },
            error: function(xhr, status, error) 
            {
                console.error(xhr.responseText);
            }
        });
    }
    getData();
});

不工作仍然显示这个:

["unit",[{"id":1,"cid":"hamz0865","unitname":"Jacqurad","unitcontact":"1010101","unitaddress":"Korangi","updated_at":"2023-04-11T15:27:47.000000Z","created_at":"2023-04-11T12:48:41.000000Z"},{"id":2,"cid":"hamz0865","unitname":"asdasd","unitcontact":"asdasdasd","unitaddress":"asdasd","updated_at":"2023-04-11T12:50:23.000000Z","created_at":"2023-04-11T12:50:23.000000Z"}]]

ajax laravel-blade laravel-10
© www.soinside.com 2019 - 2024. All rights reserved.