我知道有几篇关于使用AJAX删除的405个错误的帖子。但是,我发现的帖子中没有一个解决方案对我有用。
我有一个视图,其中一个表显示了我数据库的Machine表中的所有机器。在每一行都有一个删除按钮来删除机器,如下所示:
<button class="btn btn-small btn-danger delete-machine"
type="button"
title="Delete Machine"
machine-id="{{ $value->machineId }}">
<span class="glyphicon glyphicon-trash"></span>
</button>
点击按钮,执行以下javascript,调用控制器中的destroy方法从数据库中删除机器:
$(document).on('click', ".delete-machine", function() {
let button = $(this);
let id = $(this).attr('machine-id');
let tr = $(this).closest('tr');
let didConfirm = confirm("Are you sure you want to permanently delete this machine?");
if (didConfirm) {
$.ajax({
type: 'DELETE',
url: 'machines/' + id,
data: {
'_token' : $('input[name="_token"]').val(),
'id' : id
},
success: function(response) {
if (response.didSucceed) {
tr.remove();
let flashMessage = '<div class="alert alert-success alert-block"><button type="button" class="close" data-dismiss="alert">×</button><strong>'
+ response.message +
'</strong></div>';
$('#flash-message').append(flashMessage);
} else {
let flashMessage = '<div class="alert alert-error alert-block"><button type="button" class="close" data-dismiss="alert">×</button><strong>'
+ response.message +
'</strong></div>';
$('#flash-message').append(flashMessage);
}
},
error: function (response) {
console.log('Error:', response);
}
});
}
});
我的控制器是资源控制器。这是路线:
Route::resource('machines', 'Machine\MachineController');
这是来自被调用的控制器的destroy方法。它首先从用户订阅表中删除引用该计算机的所有行。然后,它删除机器:
public function destroy($id)
{
$userMachineSubscriptions = UserMachineSubscription::where('machine_id', $id)->get();
foreach($userMachineSubscriptions as $userMachineSubscription) {
$userMachineSubscription->delete();
}
$userMachineSubscriptions = UserMachineSubscription::where('machine_id', $id)->count();
if ($userMachineSubscriptions <= 0) {
$machine = Machine::find($id);
if ($machine->delete()) {
$message = 'Machine "' . $machine['name'] . '" successfully deleted!';
return response()->json(['didSucceed' => 'true', 'message' => $message]);
} else {
$message = 'Machine "' . $machine['name'] . '" was not deleted.';
return response()->json(['didSucceed' => 'false', 'message' => $message]);
}
} else {
$message = 'Error performing cascading delete on user subscriptions. Machine was not deleted.';
return response()->json(['didSucceed' => 'false', 'message' => $message]);
}
}
当我运行php artisan route:list时,我看到了我的路线:
Method: DELETE
URI: machines/{machine}
Middleware: web, auth
Name: machines.destroy
Action: App\Http\Controllers\Machine\MachineController@destroy
我做错了什么?使用Laravel 5.4。
...
另外,在旁注中,我是网络开发的新手,所以如果有人在我的代码中看到任何可以改进的东西,我愿意学习:)例如,我有点不喜欢我展示的方法成功的flash消息。我为几个ajax请求执行此操作,并希望我可以执行session :: flash或其他操作,而不是将html附加到主刀片布局中的div。但是,我似乎找不到更好的方法来做到这一点。
UDPATE:已解决。只需要清除路由缓存。 :(
答案 0 :(得分:3)
php artisan route:list
请参阅,如果路由中是否允许您尝试发送的DELETE请求。您还可以尝试route:clear.
清除路由缓存。
答案 1 :(得分:2)
如果使用IIS,请尝试在C:\ Windows \ System32 \ inetsrv \ config中编辑applicationHost.config。 在
中添加动词<add name="PHP_via_FastCGI1" path="*.php" verb="GET,HEAD,POST,DELETE,PATCH,PUT" modules="FastCgiModule" scriptProcessor="C:\Program Files (x86)\iis express\PHP\v7.4\php-cgi.exe" resourceType="Either" />
<add name="PHP_via_FastCGI" path="*.php" verb="GET,HEAD,POST,DELETE,PATCH,PUT" modules="FastCgiModule" scriptProcessor="C:\Program Files\PHP\v7.4\php-cgi.exe" resourceType="Either" />
<add name="PHP53_via_FastCGI" path="*.php" verb="GET,HEAD,POST,DELETE,PATCH,PUT" modules="FastCgiModule" scriptProcessor="C:\Program Files (x86)\PHP\v5.3\php-cgi.exe" resourceType="Either" />