laravel 获取 405 方法不允许异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23898349/
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
Getting a 405 method not allowed exception
提问by Gagan
I have a jquery script(downloaded from github) that deletes the entities. Following is the script.
我有一个删除实体的 jquery 脚本(从 github 下载)。以下是脚本。
$(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'));
});
});
and i use it as such
我就这样使用它
{{link_to_route('download.delete','x', ['id' => $download->id], array('class'=> 'rest-delete label label-danger')) }}
The corresponding laravel route is as follows
对应的laravel路由如下
Route::delete('/deletedownload/{id}', array('uses' => 'DownloadsController@deletedownload', 'as'=>'download.delete'));
However I am getting a 405 Method not allowed error when I try to press the X (delete button). The error is as follows
但是,当我尝试按 X(删除按钮)时,出现 405 Method not allowed 错误。错误如下
DELETE http://production:1234/deletedownload/42 405 (Method Not Allowed) .
This is working fine on my local sandbox.
这在我的本地沙箱上运行良好。
Any help will be well appreciated.
任何帮助将不胜感激。
thanks
谢谢
采纳答案by The Alpha
You have used method:DELETE
instead use following in your ajax
call
您method:DELETE
在ajax
通话中使用了以下内容
$.ajax({
headers: {...},
url: self.attr('href'),
type:"post",
data: { _method:"DELETE" },
success: function(data) {...},
error: function(data) {...}
});
Laravel
will look for the _method
in POST
and then the DELETE
request will be used if found the method.
Laravel
将查找_method
inPOST
然后DELETE
如果找到该方法将使用该请求。
Update:(Because of this answer, pointed by nietonfir
)
更新:(因为这个答案的,由尖nietonfir
)
You may try DELETE
method directly like this (if it doesn't work then try the other one), :
您可以DELETE
像这样直接尝试方法(如果它不起作用,请尝试另一个),:
$.ajax({
headers: {...},
url: self.attr('href'),
type:"DELETE",
success: function(data) {...},
error: function(data) {...}
});