我知道有几篇关于使用AJAX delete的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执行调用控制器中的销毁方法以从数据库中删除机器:
$(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工匠路由:列表,我看到我的路由:
Method: DELETE
URI: machines/{machine}
Middleware: web, auth
Name: machines.destroy
Action: App\Http\Controllers\Machine\MachineController@destroy
我做错了什么?使用Laravel 5.4。
...
另外,顺便说一句,我对web开发还不熟悉,所以如果有人看到我的代码中有什么可以改进的地方,我愿意学习:)例如,我有点不喜欢我在成功时显示即时消息的方法。我这样做是为了几个ajax请求,我希望我可以只做session::flash之类的事情,而不是在我的主刀片布局中将html附加到div中。然而,我似乎找不到更好的方法来做这件事。
乌德帕特:解决了。只需要清除路由缓存:(
php artisan route:list
请参阅,您尝试发送的删除请求是否在路由中被允许。您也可以尝试route:clear代码>以清除路由缓存。
如果使用IIS,请尝试在C:\Windows\System32\inetsrv\config中编辑应用程序Host.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" />