DataTables警告:table id = organizationTable-无效的JSON响应。拉拉韦尔

时间:2018-07-17 15:21:55

标签: jquery ajax laravel-5 datatable

当我从AJax成功更新表单并重新加载数据表时,我将收到一条错误消息,提示 DataTables警告:表id = organizationTable-无效的JSON响应。我认为原因是因为我返回了一些与数据表格式不匹配的数据。有没有一种方法可以检查数据表的json格式?因此,我可以尝试创建一个与更新用户的格式匹配的数组,并将其作为json格式返回。或我正在寻找错误的方向。谢谢你们。

控制器

public function update(Request $request, $id)
    {
        $user = User::find($id);

        request()->validate([
            'first_name' =>'required',
            'email' => ['required', Rule::unique('users')->ignore($user)],
        ]);

        $user->update($request->all());

        DB::table('model_has_roles')
            ->where('model_id', $user->id)
            ->update(['role_id' => $request->role_id]);

        $request->session()->flash('success', $user->first_name . ' is updated successfully.');

        return response()->json($user);
    }

数据表视图

<table id="organizationTable" class="table table-striped table-bordered">
        <thead>
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Role Name</th>
            <th>Status</th>
            <th>Action</th>
        </tr>
        </thead>
        <tbody>
        @foreach($rainman_users as $rainman_user)
            <tr>
                <td style="vertical-align: middle">{{$rainman_user->first_name}} {{$rainman_user->last_name}}</td>
                <td style="vertical-align: middle">{{$rainman_user->email}}</td>
                <td style="vertical-align: middle">{{$rainman_user->getRoleNames()->implode(', ')}}</td>
                @if($rainman_user->status_id == 1)
                    <td class="text-success" style="text-align: center; font-style: italic; vertical-align: middle; font-weight: bold;">{{$rainman_user->userStatus->name}}</td>
                @else
                    <td class="text-danger" style="text-align: center; font-style: italic; vertical-align: middle; font-weight: bold;">{{$rainman_user->userStatus->name}}</td>
                @endif
                <td style="text-align:center;">
                    <button data-toggle="modal" data-target="#editModal" class="edit-modal btn btn-outline-primary"
                            data-id="{{$rainman_user->id}}" data-first_name="{{$rainman_user->first_name}}"
                            data-last_name="{{$rainman_user->last_name}}" data-email="{{$rainman_user->email}}"
                            data-role_id="{{$rainman_user->getRoleIds()->first()}}"
                            data-status_id="{{$rainman_user->userStatus->id}}">
                            <i class="fa fa-pencil-square-o" aria-hidden="true"></i> Detail
                    </button>
                </td>
            </tr>
        @endforeach
        </tbody>
        <tfoot>
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Role Name</th>
            <th>Status</th>
            <th>Action</th>
        </tr>
        </tfoot>
    </table>

JavaScript

$(document).ready(function() {
    $('#organizationTable').DataTable({
    });

    $(document).on('click', '.edit-modal', function() {
        $('.modal-title').text('Edit User');
        $('#id_edit').val($(this).data('id'));
        $('#first_name_edit').val($(this).data('first_name'));
        $('#last_name_edit').val($(this).data('last_name'));
        $('#email_edit').val($(this).data('email'));
        $('#user_role_edit').val($(this).data('role_id'));
        $('#user_status_edit').val($(this).data('status_id'));
        id = $('#id_edit').val();
        $('#editModal').modal('show');
    });

    $('.modal-footer').on('click', '.edit', function() {
        $.ajax({
            type: 'PUT',
            url: '/rainman-users/' + id + '/update',
            data: {
                'id': $("#id_edit").val(),
                'first_name': $("#first_name_edit").val(),
                'last_name': $("#last_name_edit").val(),
                'email': $("#email_edit").val(),
                'role_id': $("#user_role_edit").val(),
                'status_id': $("#user_status_edit").val()
            },
            success: function(data) {
                console.log(data);
                $('#organizationTable').DataTable().ajax.reload();
            }
        });
    });
} );

0 个答案:

没有答案