【发布时间】:2018-08-08 19:49:29
【问题描述】:
我的 web.php 中定义了以下路由
Route::delete('/contributions/contribution/destroy', 'ContributionController@destroy');
在我的刀片文件中,我有一个按钮,单击该按钮会显示一个确认删除的甜蜜警报。这可以正常工作,但是当用户按下删除按钮而不是转到 Ajax 中定义的 URL 时,它会转到一个不正确的 URL 并记录以下错误:
jquery.min.js:4 DELETE http://localhost/contributions/batches/batch/edit 404(未找到)
这是按钮代码
<td class="text-center"><button type="button" class="btn btn-danger btn-sm btn-icon btn-destroy"
data-batch-id="{{ $batch->id }}"
data-id="{{ $contribution->id }}"
data-amount="${{ number_format ($contribution->contribution_amount, 2, '.', ',') }}"
data-contributor="{{ $contribution->first_name.' '.$contribution->last_name }}"
><i class="fa fa-times"></i></button>
</td>
这是我的脚本
<script>
$(document).on('click', '.btn-destroy', function (e) {
e.preventDefault();
var batchId = $(this).data('batch-id');
var id = $(this).data('id');
var amount = $(this).data('amount');
var contributor = $(this).data('contributor');
swal({
title: "Delete?",
text: "Do you want to delete contribution for "+ amount +" from "+ contributor +"?",
type: "error",
showCancelButton: true,
confirmButtonClass: 'btn-danger',
confirmButtonText: "Delete",
closeOnConfirm: false
},
function() {
$.ajax({
url: "{{ url('/contributions/contribution/destroy') }}",
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
type: "DELETE",
data: {batch:batchId, contribution:id},
success: function (data) {
console.log(data);
}
});
});
});
奇怪的是,在错误从控制器的销毁函数返回我的会话闪存消息后刷新页面
*注意:我确实在 IIS 中添加了 DELETE 动词,在此之前我收到了:
405(不允许的方法)
有什么想法吗?
【问题讨论】:
-
经过进一步审查,我确定错误来自控制器销毁功能中的返回路线。该记录正在被删除,但在返回时它正在尝试使用 DELETE 方法。为什么是这样?删除localhost/contributions/batches/batch/edit/…405(方法不允许)