Laravel Ajax 删除:405 错误 - 方法不允许
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45722682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Ajax Delete: 405 Error - Method Not Allowed
提问by JonTan
I know there are several posts about 405 Errors from using AJAX delete. However, none of the solutions from the posts I found worked for me.
我知道有几篇关于使用 AJAX 删除的 405 错误的帖子。但是,我发现的帖子中的所有解决方案都不适合我。
I have a view with a table displaying all the machines from my database's Machine table. On each row there is a delete button to delete the machine like so:
我有一个表,其中显示了我数据库的 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>
On button click, the following javascript executes which calls the destroy method in the controller to delete the machine from the database:
单击按钮时,将执行以下 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);
}
});
}
});
My controller is a resource controller. Here is the route:
我的控制器是资源控制器。这是路线:
Route::resource('machines', 'Machine\MachineController');
Here is the destroy method from the controller being called. It first deletes all the rows referencing the machine from a user subscription table. Then, it deletes the machine:
这是来自被调用的控制器的销毁方法。它首先从用户订阅表中删除所有引用机器的行。然后,它删除机器:
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]);
}
}
When I run php artisan route:list, I see my route:
当我运行 php artisan route:list 时,我看到了我的路线:
Method: DELETE
URI: machines/{machine}
Middleware: web, auth
Name: machines.destroy
Action: App\Http\Controllers\Machine\MachineController@destroy
What am I doing wrong? Using Laravel 5.4.
我究竟做错了什么?使用 Laravel 5.4。
...
...
Also, on a side note, I am new to web-development so if anyone sees anything in my code that can be improved someway, I am open to learning :) For example, I kind of dislike my method of showing a flash message on success. I do this for several ajax requests and wish I could just do session::flash or something instead of appending the html to a div in my master blade layout. However, I can't seem to find a better way to do it.
另外,顺便提一下,我是网络开发的新手,所以如果有人看到我的代码中有任何可以改进的地方,我愿意学习:) 例如,我有点不喜欢我在上面显示 Flash 消息的方法成功。我为几个 ajax 请求执行此操作,并希望我可以只执行 session::flash 或其他操作,而不是将 html 附加到我的主刀片布局中的 div。但是,我似乎找不到更好的方法来做到这一点。
UDPATE: Solved. Just needed to clear the route cache. :(
UDPATE:已解决。只需要清除路由缓存。:(
回答by Casper
php artisan route:list
See, If the DELETE request you are trying to send is allowed in the routes or not. You can also try to route:clear.
to clear the route cache.
请参阅,您尝试发送的 DELETE 请求是否在路由中被允许。您也可以尝试route:clear.
清除路由缓存。
回答by Ibray
If you use IIS try edit applicationHost.config in C:\Windows\System32\inetsrv\config. Add verbs in
如果您使用 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" />