获取405方法不允许例外

我有一个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')); 

});

我就这样使用它

 {{link_to_route('download.delete','x', ['id' => $download->id], array('class'=> 'rest-delete label label-danger')) }} 

相应的laravel路线如下

 Route::delete('/deletedownload/{id}', array('uses' => 'DownloadsController@deletedownload', 'as'=>'download.delete')); 

但是当我尝试按X(删除按钮)时,我得到405方法不允许错误。 错误如下

 DELETE http://production:1234/deletedownload/42 405 (Method Not Allowed) . 

这在我当地的沙箱上工作正常。

任何帮助将不胜感激。

谢谢

您已经使用了method:DELETE而是在您的ajax调用中使用以下内容

 $.ajax({ headers: {...}, url: self.attr('href'), type:"post", data: { _method:"DELETE" }, success: function(data) {...}, error: function(data) {...} }); 

Laravel将在POST查找_method ,如果找到该方法,则将使用DELETE请求。

更新:( 由于这个答案 , nietonfir指出)

您可以像这样直接尝试DELETE方法(如果它不起作用,那么尝试另一个),:

 $.ajax({ headers: {...}, url: self.attr('href'), type:"DELETE", success: function(data) {...}, error: function(data) {...} });