Laravel-5.4:使用Ajax更新帖子

时间:2018-11-20 15:09:44

标签: php ajax laravel-5.4

首先让我展示代码,这是我的html模式

null

我的更新控制器

<div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title"></h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label class="control-label col-sm-2" for="city-id">City id:</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="city-id" disabled>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-sm-2" for="city-name">City name:</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="city-name">
                            </div>
                        </div>
                    </form>
                    <div class="deleteContent">
                        Are you Sure you want to delete <span class="dname"></span> ? <span
                                class="hidden did"></span>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn actionBtn" data-dismiss="modal">
                            <span id="footer_action_button" class='glyphicon'> </span>
                        </button>
                        <button type="button" class="btn btn-warning" data-dismiss="modal">
                            <span class='glyphicon glyphicon-remove'></span> Close
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

我正在尝试更新记录,这是我在ajax中所做的

public function update( Request $request, City $data ) {
        $input = $request->all();
        $data->fill( $input )->save();

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

在控制台中,我可以看到$('.modal-footer').on('click', '.edit', function () { $.ajax({ url: $("#city-id").val(), method: 'PATCH', data: { '_token': $('input[name=_token]').val(), 'id': $("#city-id").val(), 'name': $('#city-name').val() }, success: function (data) { $('.item' + data.id).replaceWith("<tr class='item" + data.id + "'><td>" + data.id + "</td><td>" + data.name + "</td><td><button class='edit-modal btn btn-info' data-id='" + data.id + "' data-name='" + data.name + "'><span class='glyphicon glyphicon-edit'></span> Edit</button> <button class='delete-modal btn btn-danger' data-id='" + data.id + "' data-name='" + data.name + "' ><span class='glyphicon glyphicon-trash'></span> Delete</button></td></tr>"); } }); }); Request URL: http://localhost:8000/admin/cities/12,也可以尝试Request method: PUTPATCH

但是它添加新记录而不是更新记录。

我想用这种方式来做Ajax,但也不能正常工作

Status code: 200

0 个答案:

没有答案