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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:33:48  来源:igfitidea点击:

Getting a 405 method not allowed exception

jquerylaravel

提问by Gagan

I have a jquery script(downloaded from github) that deletes the entities. Following is the script.

我有一个删除实体的 jquery 脚本(从 gi​​thub 下载)。以下是脚本。

$(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:DELETEinstead use following in your ajaxcall

method:DELETEajax通话中使用了以下内容

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

Laravelwill look for the _methodin POSTand then the DELETErequest will be used if found the method.

Laravel将查找_methodinPOST然后DELETE如果找到该方法将使用该请求。

Update:(Because of this answer, pointed by nietonfir)

更新:因为这个答案的,由尖nietonfir

You may try DELETEmethod 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) {...}
});