获取 405 method not allowed 异常

2023-12-08

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

});

我这样用它

{{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 Method not allowed 错误。错误如下

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

这在我的本地沙箱上运行良好。

任何帮助将不胜感激。

thanks


您已使用过method:DELETE而是在你的中使用以下内容ajax call

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

Laravel将寻找_method in POST然后是DELETE如果找到该方法,将使用请求。

Update: (因为这个答案,由nietonfir)

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

$.ajax({
    headers: {...},
    url: self.attr('href'),
    type:"DELETE",
    success: function(data) {...},
    error: function(data) {...}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取 405 method not allowed 异常 的相关文章

随机推荐