问题描述
我有一个jquery脚本(从github下载),它删除了实体.以下是脚本.
I have a jquery script(downloaded from github) that deletes the entities. Following is the script.
$(document).ready(function() {
var restful = {
init: function(elem) {
elem.on('click', function(e) {
self=$(this);
e.preventDefault();
if(confirm('Are you sure you want to delete this record ? Note : The record will be deleted permanently from the database!')) {
$.ajax({
headers: {
Accept : "text/plain; charset=utf-8",
"Content-Type": "text/plain; charset=utf-8"
},
url: self.attr('href'),
method: 'DELETE',
success: function(data) {
self.closest('li').remove();
},
error: function(data) {
alert("Error while deleting.");
console.log(data);
}
});
}
})
}
};
restful.init($('.rest-delete'));
});
我就这样使用
{{link_to_route('download.delete','x', ['id' => $download->id], array('class'=> 'rest-delete label label-danger')) }}
对应的laravel路线如下
The corresponding laravel route is as follows
Route::delete('/deletedownload/{id}', array('uses' => 'DownloadsController@deletedownload', 'as'=>'download.delete'));
但是,当我尝试按X(删除按钮)时,出现405方法不允许错误.错误如下
However I am getting a 405 Method not allowed error when I try to press the X (delete button). The error is as follows
DELETE http://production:1234/deletedownload/42 405 (Method Not Allowed) .
这在我的本地沙箱上运行正常.
This is working fine on my local sandbox.
任何帮助将不胜感激.
Any help will be well appreciated.
谢谢
推荐答案
您在ajax
调用中使用了method:DELETE
而不是以下内容
You have used method:DELETE
instead use following in your ajax
call
$.ajax({
headers: {...},
url: self.attr('href'),
type:"post",
data: { _method:"DELETE" },
success: function(data) {...},
error: function(data) {...}
});
Laravel
将在POST
中查找_method
,然后如果找到该方法,则将使用DELETE
请求.
Laravel
will look for the _method
in POST
and then the DELETE
request will be used if found the method.
更新:(,由nietonfir
指出)
您可以像这样直接尝试DELETE
方法(如果不起作用,请尝试另一个方法),:
You may try DELETE
method directly like this (if it doesn't work then try the other one), :
$.ajax({
headers: {...},
url: self.attr('href'),
type:"DELETE",
success: function(data) {...},
error: function(data) {...}
});
这篇关于获取405方法不允许的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!