错误405:方法不允许,EditUser路由无法获取信息

时间:2018-06-04 11:14:24

标签: laravel datatable

所以我一直在学习Laravel,并开始制作DataTables。但是,当我点击“编辑”按钮时,我的'editItem'路径无法获取任何信息。它显示错误405。

DataTable视图(dt.blade.php) -

显示表格的HTML部分

<div class="table-responsive text-center">
    <table class="table table-borderless" id="table">
        <thead>
            <tr>
                <th class="text-center">ID</th>
                <th class="text-center">Name</th>
                <th class="text-center">Created At</th>
                <th class="text-center">Updated At</th>
                <th class="text-center">Actions</th>
            </tr>
        </thead>
        @foreach($users as $user)
        <tr class="user{{$user->id}}">
            <td >{{$user->id}}</td>
            <td>{{$user->name}}</td>
            <td>{{$user->created_at}}</td>
            <td>{{$user->updated_at}}</td>
            <td><button class="edit-modal btn btn-info"
                    value="{{$user->id}},{{$user->name}}">
                    <span class="glyphicon glyphicon-edit"></span> Edit
                </button>
                <button class="delete-modal btn btn-danger"
                    value="{{$user->id}},{{$user->name}}">
                    <span class="glyphicon glyphicon-trash"></span> Delete
                </button></td>
        </tr>
        @endforeach
    </table>
</div>

JS部分在同一个文件中

    <script>
    $(document).ready(function() {
    $('#table').DataTable();
    } );
    </script>

    <script>

    $(document).on('click', '.edit-modal', function() {
        $('#footer_action_button').text("Update");
        $('#footer_action_button').addClass('glyphicon-check');
        $('#footer_action_button').removeClass('glyphicon-trash');
        $('.actionBtn').addClass('btn-success');
        $('.actionBtn').removeClass('btn-danger');
        $('.actionBtn').removeClass('delete');
        $('.actionBtn').addClass('edit');
        $('.modal-title').text('Edit');
        $('.deleteContent').hide();
        $('.form-horizontal').show();
        var stuff = $(this).val().split(',');
        console.log($(this).val());
        fillmodaluser(stuff)
        $('#myModal').modal('show');
    });
    $(document).on('click', '.delete-modal', function() {
        $('#footer_action_button').text(" Delete");
        $('#footer_action_button').removeClass('glyphicon-check');
        $('#footer_action_button').addClass('glyphicon-trash');
        $('.actionBtn').removeClass('btn-success');
        $('.actionBtn').addClass('btn-danger');
        $('.actionBtn').removeClass('edit');
        $('.actionBtn').addClass('delete');
        $('.modal-title').text('Delete');
        $('.deleteContent').show();
        $('.form-horizontal').hide();
        var stuff = $(this).val().split(',');
        console.log($(this).val('info'));
        $('.did').text(stuff[0]);
        $('.dname').html(stuff[1]);
        $('#myModal').modal('show');
    });

    function fillmodaluser(details){
    $('#fid').val(details[0]);
    $('#name').val(details[1]);
    }

    $('.modal-footer').on('click', '.edit', function() {

        $.ajax({
            type: 'post',
            url: '../public/editUser',
            user: {
                '_token': $('input[name=_token]').val(),
                'id': $('#fid').val(),
                'name': $('#name').val()
            },
            success: function(user) {
                if (user.errors){
                    $('#myModal').modal('show');
                    if(user.errors.name) {
                        $('.name_error').removeClass('hidden');
                        $('.name_error').text("Name can't be empty !");
                    }
                    if(user.errors.email) {
                        $('.email_error').removeClass('hidden');
                        $('.email_error').text("Email must be a valid one !");
                    }
                }
                 else {

                     $('.error').addClass('hidden');
                $('.user' + users.id).replaceWith("<tr class='users" + users.id + "'><td>" +users.id + "</td><td>" + users.name+"</td><td>" + "</td><td>" + "</td><td><button class='edit-modal btn btn-info' user-info='" + users.id+","+users.name+"'><span class='glyphicon glyphicon-edit'></span> Edit</button> <button class='delete-modal btn btn-danger' user-info='" + users.id+","+users.name+"' ><span class='glyphicon glyphicon-trash'></span> Delete</button></td></tr>");

                 }}
        });
    });
    $('.modal-footer').on('click', '.delete', function() {

        $.ajax({
            type: 'post',
            url: '../public/deleteUser',
              headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            user: {
                '_token': $('input[name=_token]').val(),
                'id': $('.did').text()
            },
            success: function(user) {
                $('.user' + $('.did').text()).remove();
            }
        });
    });

</script>

最后,web.php

Route::get('/dt', 'UserController@dt');    
Route::get('/editUser', function (Request $request) {
    $rules = array (
            'name' => 'required|alpha',     
    );
    $validator = Validator::make(Input::all(), $rules );
    if ($validator->fails ())
        return Response::json ( array (
            'errors' => $validator->getMessageBag()->toArray () 
        ) );
    else {
        $user->id = User::find ( $request->id );
        $user->name = ($request->name);
        $user->save ();
        return response ()->json ( $user );
    }
});
Route::get ('/deleteUser', function (Request $request) {
    User::find ( $request->id )->delete ();
    return response ()->json ();
});

向我显示的JSON错误是“{"errors":{"name":["The name field is required."]}}

我的问题是,当我按下编辑按钮时,可以在JQuery中拆分信息;它能够显示我选择的行的信息。我不明白为什么没有相同的值被发送到我的editUser路由?

2 个答案:

答案 0 :(得分:0)

将所有get方法更改为post,因为您在客户端使用post

    Route::post('/dt', 'UserController@dt');    
Route::post('/editUser', function (Request $request) {
    $rules = array (
            'name' => 'required|alpha',     
    );
    $validator = Validator::make(Input::all(), $rules );
    if ($validator->fails ())
        return Response::json ( array (
            'errors' => $validator->getMessageBag()->toArray () 
        ) );
    else {
        $user->id = User::find ( $request->id );
        $user->name = ($request->name);
        $user->save ();
        return response ()->json ( $user );
    }
});
Route::post ('/deleteUser', function (Request $request) {
    User::find ( $request->id )->delete ();
    return response ()->json ();
});

答案 1 :(得分:0)

似乎您的网址不应该是&#39; ../ public /&#39; (在您的ajax请求中)但它应与您在文件web.php

中输入的内容相匹配