<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">×</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>
我的更新控制器
public function update( Request $request, City $data ) {
$input = $request->all();
$data->fill( $input )->save();
return response()->json( $data );
}
我正在尝试更新记录,下面是我在ajax中所做的
$('.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: PUT
PATCH
Status code: 200
但它会添加新记录,而不是更新记录。
我想这样做Ajax,但不太管用
$(document).on('click', '.edit', function () {
$.ajax({
url: {{ cities.update }},
type: 'PUT', //OR PUTCH
data: $('#myModal').serialize(),
success: function (result) {
console.log('success');
},
error: function (data) {
// you'd want to show your validation errors if there are any, as well
console.log(data);
}
});
});